通过存根实体进行实体框架代码首次更新
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您正在寻找ApplyCurrentValues
ApplyCurrentValues 将标量值从现有对象复制到图中的对象(在上述情况下 - 存根实体)。
来自 MSDN 上的备注部分:
这就是你的追求吗?
I think you are looking for ApplyCurrentValues
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:
Is that what your after?
为了构建 Paul 的答案,当您使用 EF 模型或数据库优先时,以下内容将起作用:
To build on Paul's answer, the following will work when you are using EF Model or Database First:
我认为您正在寻找 Attach() 方法。
I think you are looking for the Attach() method.
也许可以尝试一下,它是 EF Code First 特有的,它的做法似乎与 EF 不同。
来自 ADO.NET 团队博客
“将属性标记为已修改会强制在调用 SaveChanges 时向数据库发送该属性的更新,即使该属性的当前值是与其原始值相同。”
Try this maybe, it is specific to EF Code First which seems to do it differently than just EF.
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."