这是从 DataContext 更新实体的最佳方法吗?

发布于 2024-10-29 16:50:12 字数 529 浏览 1 评论 0原文

我发现这是从 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 技术交流群。

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

发布评论

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

评论(3

悟红尘 2024-11-05 16:50:12

您可以使用 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.

孤凫 2024-11-05 16:50:12

您可以附加实体并更改其对象状态条目...

context.Leads.Attach(lead);

ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(lead);
entry.ChangeState(EntityState.Modified);
context.SaveChanges();

You can attach the entity and change it's object state entry...

context.Leads.Attach(lead);

ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(lead);
entry.ChangeState(EntityState.Modified);
context.SaveChanges();
花桑 2024-11-05 16:50:12

你可以通过反思来做到这一点。这是我为此目的编写的一种类似方法:

    public static FMVHistory CloneFMV(FMVHistory F) {
        FMVHistory F_Clone = new FMVHistory();

        Type typeToClone = F.GetType();
        Type[] BadGenericTypes = new Type[] { typeof(EntityCollection<>), typeof(EntityReference<>) };
        Type[] BadTypes = new Type[] { typeof(System.Data.EntityKey) };

        foreach (PropertyInfo pi in typeToClone.GetProperties().Where(p => p.CanWrite)) {
            if (pi.PropertyType.IsGenericType && BadGenericTypes.Contains(pi.PropertyType.GetGenericTypeDefinition())
                || (BadTypes.Contains(pi.PropertyType))
                || (pi.Name.Equals("nameOfYourPrimaryKeyWhichYouDontWantCloned"), StringComparison.CurrentCultureIgnoreCase)))
                continue;

            pi.SetValue(F_Clone, pi.GetValue(F, null), null);
        }
        return F_Clone;
    }

除了传递一个要克隆的对象之外,您还可以传递一个源对象和一个目标对象,并将值从一个对象复制到另一个对象。

You can do that with reflection. Here's a similar method I wrote for that purpose:

    public static FMVHistory CloneFMV(FMVHistory F) {
        FMVHistory F_Clone = new FMVHistory();

        Type typeToClone = F.GetType();
        Type[] BadGenericTypes = new Type[] { typeof(EntityCollection<>), typeof(EntityReference<>) };
        Type[] BadTypes = new Type[] { typeof(System.Data.EntityKey) };

        foreach (PropertyInfo pi in typeToClone.GetProperties().Where(p => p.CanWrite)) {
            if (pi.PropertyType.IsGenericType && BadGenericTypes.Contains(pi.PropertyType.GetGenericTypeDefinition())
                || (BadTypes.Contains(pi.PropertyType))
                || (pi.Name.Equals("nameOfYourPrimaryKeyWhichYouDontWantCloned"), StringComparison.CurrentCultureIgnoreCase)))
                continue;

            pi.SetValue(F_Clone, pi.GetValue(F, null), null);
        }
        return F_Clone;
    }

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.

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