gpt4 book ai didi

asp.net-mvc - 干净地更新 Entity Framework 中的层次结构

转载 作者:行者123 更新时间:2023-12-03 22:45:36 25 4
gpt4 key购买 nike

我正在提交 StoredProcedureReport 的表格,其中有很多 StoredProcedureParameters . Create 工作正常,但尝试更新让我问微软是否可以认真对待。

我来自 Rails 背景,其中 @report.update_attributes(params[:report])将确切地知道如何处理它在其中找到的任何关联数据。据我所知,与此等效的 .NET 是 TryUpdateModel ,这看起来很有希望。首先。所以我尝试了一些像这样的参数

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

我假设设置了所有主键和外键后,EF 将知道如何更新 StoredProcedureReportParameter当我这样做时的对象:
var report = context.StoredProcedureReports.FirstOrDefault(r => r.IDGUID == reportID);

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

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

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.



此消息中的建议之一是我应该为外键属性分配一个非空值,但正如我所说, StoredProcedureReportIDStoredProcedureReportParameter 上都有正确的值对象。

我读过的其他帖子涉及 Update操作循环关联并将它们附加到上下文。这真的是我在做的事情吗? EF真的那么密集吗?我希望 .NET 专业人士能在这里向我展示光芒。必须有比这更简单的方法。

最佳答案

这并不难。使用 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 Controller ;所以,你必须告诉 EF StoredProcedureReportStoredProcedureParameters被修改。

如果正在修改所有属性,您可以使用:
ctx.Entry(foo).State = EntityState.Modified;
//remember to do the same in all children objects

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

如果不是所有属性都被修改,您必须指定哪些属性是。像这样:
context.Entry(foo).Property("Description").IsModified = true; 
context.Entry(foo).Property("AnotherProperty").IsModified = true;

希望能帮助到你!

关于asp.net-mvc - 干净地更新 Entity Framework 中的层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32404672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com