LINQ2SQL 实体 - 仅更新已更改的字段
我希望在我的 MVC 3 项目中有一种更简单的方法来做到这一点。在我的数据库中,我有一个通过 LINQ2SQL 映射到我的应用程序中的 Customer 表。还有一个部分客户类,我在其中执行更新、查找等 - 其中我有一个像这样的更新方法:
public static void Update(Customer customer)
{
if (customer == null)
return;
using(var db = new DataBaseContext)
{
var newCustomer = db.Customers.Where(c => c.customer_id = customer.customer_id).SingleOrDefault();
if(newCustomer == null)
return;
newCustomer.first_nm = customer.first_nm;
// ...
// ... Lot's of fields to update
// ...
newCustomer.phone_num = customer.phone_nm;
db.SubmitChanges();
}
}
我希望找到一种不太麻烦的方法,用中的相应字段更新 newCustomer 中的字段不同的客户。
有什么建议吗?谢谢。
I was hoping there was an easier way to do this in my MVC 3 project. In my database, I have a Customer table that is mapped in my application via LINQ2SQL. There is also a partial customer class where I perform updates, look-up etc - which where I have an update method like this:
public static void Update(Customer customer)
{
if (customer == null)
return;
using(var db = new DataBaseContext)
{
var newCustomer = db.Customers.Where(c => c.customer_id = customer.customer_id).SingleOrDefault();
if(newCustomer == null)
return;
newCustomer.first_nm = customer.first_nm;
// ...
// ... Lot's of fields to update
// ...
newCustomer.phone_num = customer.phone_nm;
db.SubmitChanges();
}
}
What I was hoping to find was a less-cumbersome method to update the fields in newCustomer with the corresponding fields in customer that are different.
Any suggestions? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以实现 iequalityComparer :
,并按照以下操作:或实现
iClonable
并设置newcustomer
tocustomer.clone()
。然后,无需附加customer
,因为newcustomer
已经连接。在EF(4.1)中,我认为您只需要修改的实体:
更新:
好吧,似乎L2S需要实体的原始价值。在回复您的评论时,您有几个选择:使用时间戳列,返回一部分实体或将原始实体放在手中。在您的情况下,您已经拥有原始实体:
因此,您很可能可以做:
注意:我将重命名
newcustomer
Original -Customer
如果我是您,因为它更相关到实体的状态。这种方法的问题在于,您需要额外的数据库来获取原始客户(
newCustomer
在代码中)。看看在这里//geekswithblogs.net/michelotti/archive/2007/12/17/117791.aspx“ rel =“ nofollow”>在这里肯定此处查看如何使用时间戳列来防止额外的数据库跳闸。I think you can implement IEqualityComparer:
and do it as follows:Or implement
ICloneable
and setnewCustomer
tocustomer.Clone()
. then there's no need to attachcustomer
sincenewCustomer
is already attached.in EF(4.1), I think You just have to attach the entity as modified:
UPDATE:
Well, it seems like L2S needs original values of the entity. In reply to your comment, you have a couple choices: Using a timestamp column, returning a subset of entities, or having the original entity in your hand. In your scenario, you have the original entity already:
So you will most probably can do:
Note: I'd rename
newCustomer
tooriginalCustomer
if I were you since it's more related to the entity's state.The problem with this approach is that you have an extra trip to database to get your original customer (
newCustomer
in your code). Take a look at here, here and definitely here to see how you can use TimeStamp columns to prevent the extra database trip.