通过存根实体进行实体框架代码首次更新

发布于 2024-10-07 17:09:29 字数 692 浏览 2 评论 0原文

是否可以在 EF Code-First 中进行更新,而无需使用存根对象查询数据库中的整个行,...

例如

public class Dinner 
{     
    public int DinnerID { get; set; }     
    public string Title { get; set; }     
    public DateTime EventDate { get; set; }     
    public string Address { get; set; }     
    public string Country { get; set; }     
    public string HostedBy { get; set; }       
}



Dinner dinner = dinner.DinnerID = 1;     
dinner.HostedBy = "NameChanged" 
nerdDinners.SaveChanges();

上面的代码是否会创建一个更新语句,该语句将使 DiningID 1 行的以下列为空?

标题、活动日期、地址、国家/地区

是否有一种方式或方法,例如“PropertyModified”= true,然后其余部分使它们= false,以便 HostedBy 是唯一会更新的方法?

Is it possible in EF Code-First to update without querying the entire row in db by using stub objects,...

e.g.

public class Dinner 
{     
    public int DinnerID { get; set; }     
    public string Title { get; set; }     
    public DateTime EventDate { get; set; }     
    public string Address { get; set; }     
    public string Country { get; set; }     
    public string HostedBy { get; set; }       
}



Dinner dinner = dinner.DinnerID = 1;     
dinner.HostedBy = "NameChanged" 
nerdDinners.SaveChanges();

will the code above create an Update Statement which will make the following columns null for the row of DinnerID 1 ?

Title, EventDate, Address, Country

Is there a way or method like "PropertyModified" = true, then the rest make them = false, so that HostedBy is the only one that will be updated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最笨的告白 2024-10-14 17:09:29

我认为您正在寻找ApplyCurrentValues

public void UpdateDinner(Dinner existingDinner)
{
    var stub = new Dinner { DinnerId = existingDinner.DinnerId };
    ctx.Dinners.Attach(stub);
    ctx.ApplyCurrentValues(existingDinner);
    ctx.SaveChanges();
}

ApplyCurrentValues 将标量值从现有对象复制到图中的对象(在上述情况下 - 存根实体)。

来自 MSDN 上的备注部分:

任何与对象原始值不同的值都被标记为已修改。

这就是你的追求吗?

I think you are looking for ApplyCurrentValues

public void UpdateDinner(Dinner existingDinner)
{
    var stub = new Dinner { DinnerId = existingDinner.DinnerId };
    ctx.Dinners.Attach(stub);
    ctx.ApplyCurrentValues(existingDinner);
    ctx.SaveChanges();
}

ApplyCurrentValues copies the scalar values from the existing object to the object in the graph (in the above case - the stub entity).

From the Remarks section on MSDN:

Any values that differ from the original values of the object are marked as modified.

Is that what your after?

断桥再见 2024-10-14 17:09:29

为了构建 Paul 的答案,当您使用 EF 模型或数据库优先时,以下内容将起作用:

context.ObjectStateManager.GetObjectStateEntry(dinner).SetModifiedProperty("HostedBy");

To build on Paul's answer, the following will work when you are using EF Model or Database First:

context.ObjectStateManager.GetObjectStateEntry(dinner).SetModifiedProperty("HostedBy");
烟若柳尘 2024-10-14 17:09:29

我认为您正在寻找 Attach() 方法。

I think you are looking for the Attach() method.

司马昭之心 2024-10-14 17:09:29

也许可以尝试一下,它是 EF Code First 特有的,它的做法似乎与 EF 不同。

var dinner = context.Dinners.Find(1);

context.Entry(dinner).Property(d => d.HostedBy).IsModified = true;

context.SaveChanges();

来自 ADO.NET 团队博客

“将属性标记为已修改会强制在调用 SaveChanges 时向数据库发送该属性的更新,即使该属性的当前值是与其原始值相同。”

Try this maybe, it is specific to EF Code First which seems to do it differently than just EF.

var dinner = context.Dinners.Find(1);

context.Entry(dinner).Property(d => d.HostedBy).IsModified = true;

context.SaveChanges();

From ADO.NET team blog

"Marking a property as modified forces an update to be send to the database for the property when SaveChanges is called even if the current value of the property is the same as its original value."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文