这是从 DataContext 更新实体的最佳方法吗?
我发现这是从 DataContext 更新实体的方法
public bool UpdateLead(Lead lead)
{
OrganizationServiceContext context = GetOrgContext();
Lead leadToTrack = getLead(lead.Id, context);
leadToTrack.AccountId.Id = lead.AccountId.Id;
//...
context.UpdateObject(leadToTrack);
context.SaveChanges();
return true;
}
,但该实体中有大约 200 个字段(感谢 Microsoft Dynamics CRM)。我是否必须编写 200 行,例如 leadToTrack.Field1 = Lead.Field1
还是有更简洁的方法?
谢谢
I've found that this is the way to update an entity from a DataContext
public bool UpdateLead(Lead lead)
{
OrganizationServiceContext context = GetOrgContext();
Lead leadToTrack = getLead(lead.Id, context);
leadToTrack.AccountId.Id = lead.AccountId.Id;
//...
context.UpdateObject(leadToTrack);
context.SaveChanges();
return true;
}
But I have about 200 fields in that entity (thanks to Microsoft Dynamics CRM). Do I have to write 200 lines like leadToTrack.Field1 = lead.Field1
or is there a more concise way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 AutoMapper 来实现此目的 - 如果您有那么多属性,并且双方基本上都具有相同的名称应该适合你。
You could use AutoMapper for this - if you have that many properties that all basically have the same name on both sides this should work well for you.
您可以附加实体并更改其对象状态条目...
You can attach the entity and change it's object state entry...
你可以通过反思来做到这一点。这是我为此目的编写的一种类似方法:
除了传递一个要克隆的对象之外,您还可以传递一个源对象和一个目标对象,并将值从一个对象复制到另一个对象。
You can do that with reflection. Here's a similar method I wrote for that purpose:
Except instead of passing in one object to be cloned, you'd pass in a source object, and a destination object, and copy the values over from one to the other.