在实体框架中干净地更新层次结构

只要

我正在为提交一份表格StoredProcedureReport,其中有很多表格StoredProcedureParameters创建工作正常,但尝试进行更新使我问微软是否可以认真对待。

我来自Rails的背景,他@report.update_attributes(params[:report])会确切地知道如何处理在其中找到的任何关联数据。据我所知,相当于.NET的.NETTryUpdateModel看起来很有希望。首先。所以我尝试了一些这样的参数

IDGUID:d70008a5-a1a3-03d2-7baa-e39c5044ad41
StoredProcedureName:GetUsers
Name:search again UPDATED
StoredProcedureReportParameters[0].IDGUID:d70008a5-aba3-7560-a6ef-30a5524fac72
StoredProcedureReportParameters[0].StoredProcedureReportID:d70008a5-a1a3-03d2-7baa-e39c5044ad41
StoredProcedureReportParameters[0].Name:RowsPerPage
StoredProcedureReportParameters[0].Label:rows
StoredProcedureReportParameters[0].StoredProcedureReportParameterDataTypeID:a50008a5-2755-54c0-b052-865abf459f7f
StoredProcedureReportParameters[0].StoredProcedureReportParameterInputTypeID:a50008a5-2955-a593-d00f-00cd4543babf
StoredProcedureReportParameters[0].DefaultValue:10
StoredProcedureReportParameters[0].AllowMultiple:false
StoredProcedureReportParameters[0].Hidden:false
StoredProcedureReportParameters[1].IDGUID:d70008a5-a7a3-e35e-28b6-36dd9e448ee5
StoredProcedureReportParameters[1].StoredProcedureReportID:d70008a5-a1a3-03d2-7baa-e39c5044ad41
StoredProcedureReportParameters[1].Name:PageNumber
StoredProcedureReportParameters[1].Label:page was MODIFIEIIEIEIED!!!
StoredProcedureReportParameters[1].StoredProcedureReportParameterDataTypeID:a50008a5-2755-54c0-b052-865abf459f7f
StoredProcedureReportParameters[1].StoredProcedureReportParameterInputTypeID:a50008a5-2955-a593-d00f-00cd4543babf
StoredProcedureReportParameters[1].DefaultValue:1
StoredProcedureReportParameters[1].AllowMultiple:false
StoredProcedureReportParameters[1].Hidden:false

我假设设置了所有主键和外键后,EFStoredProcedureReportParameter在执行此操作时就会知道如何更新对象:

var report = context.StoredProcedureReports.FirstOrDefault(r => r.IDGUID == reportID);

if (report != null)
{
    succeeded = TryUpdateModel(report);
    context.SaveChanges();
}

现在,如果我在上放置了一个断点context.SaveChanges(),那么我的report对象及其关联的StoredProcedureReportParameters外观就如同我期望的那样。设置外键和主键,所有值都检出。但是SaveChanges会引发此错误:

操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系。对关系进行更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

此消息中的建议之一是,我应该为外键属性分配一个非空值,但是正如我所说,这两个对象StoredProcedureReportID 具有正确的值StoredProcedureReportParameter

我读过的其他有关操作的文章都Update遍历关联并将它们附加到上下文中。这真的是我坚持要做的吗?EF真的那么密集吗?我希望有一个.NET专业人士向我展示一下。比这更简单的方法。

法比奥·卢兹(Fabio Luz)

这并不难。使用EF的主要方法有两种:附加实体和分离实体。

假设我们有2个实体:

public class Foo
{
     public int FooId { get; set; }

     public string Description { get; set; }

     public ICollection<Bar> Bars { get; set; }
 }

public class Bar
{
    public int BarId { get; set; }

    public string Description { get; set; }
}

插入

var foo = new Foo()
{
    FooId = 1,
    Description = "as",
    Bars = new List<Bar>()
    {
        new Bar
        {
            BarId = 1,
            Description = "as"
        },
        new Bar
        {
            BarId = 2,
            Description = "as"
        },
        new Bar
        {
            BarId = 2,
            Description = "as"
        }
    }
};

ctx.Foos.Add(foo);
ctx.SaveChanges();

在上面的示例中,EF将识别新项目,并将所有项目插入。

更新(已附加)

var foo = ctx.Foos.Include("Bars").Where(i => i.FooId == 1).FirstOrDefault();
foreach (var bar in foo.Bars)
{
    bar.Description = "changed";
}

ctx.SaveChanges();

在这里,我们从上下文中加载了foo及其栏。它们已经附加到上下文中。因此,我们所需要做的就是更改值并调用SaveChanges()一切都会正常。

更新(已删除)

var foo = new Foo
{
    FooId = 1,
    Description = "changed3",
    Bars = new List<Bar>
    {
        new Bar
        {
            BarId = 1,
            Description = "changed3"
        },
        new Bar
        {
            BarId = 2,
            Description = "changed3"
        }
    }
};

ctx.Entry(foo).State = EntityState.Modified;

foreach (var bar in foo.Bars)
{
    ctx.Entry(bar).State = EntityState.Modified;
}

ctx.SaveChanges();

在这里,我们正在处理数据库中已经存在的项目。但是,它们不是从EF加载的(未连接)。EF对他们一无所知。我们需要手动附加所有这些文件,并告诉EF它们已被修改。

移除(已安装)

var foo = ctx.Foos.Include("Bars").Where(i => i.FooId == 1).FirstOrDefault();
var bar = foo.Bars.First();
foo.Bars.Remove(bar);
ctx.SaveChanges();

从EF加载钢筋,然后从集合中删除它们。

删除(已删除)

var bar = new Bar
{
    BarId = 1
};

ctx.Entry(bar).State = EntityState.Deleted;
ctx.SaveChanges();

在这里,Bar并未从上下文中加载。因此,我们必须告诉EF它已被删除。


在您的情况下,您要将更新的对象发送到MVC控制器。因此,您必须告诉EFStoredProcedureReport并且StoredProcedureParameters已被修改。

如果所有属性都被修改,则可以使用:

ctx.Entry(foo).State = EntityState.Modified;
//remember to do the same in all children objects

它将所有属性标记为已修改。请注意,如果未在视图上设置某些属性,它将被更新为空值。

如果不是所有的属性都被修改,则必须指定要修改的属性。像这样:

context.Entry(foo).Property("Description").IsModified = true; 
context.Entry(foo).Property("AnotherProperty").IsModified = true; 

希望能帮助到你!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章