c# - 具有快照更改检测的 Entity Framework 中的 POCO

标签 c# entity-framework poco

我有一组 Visual Studio 2010 生成的 POCO 类(刚好从 DB Schema 生成的开箱即用的 POCO 模板)。对于给定的用例,我让用户加载一个实体(CRM 联系人)并对其进行操作 - 添加电话号码(它本身是一个由外键关联的单独实体)和地址(也是一个单独的实体)等。在两者之间回发我将修改后的实体存储在 ViewState 中(我不想立即将更改保存到数据库中)。当用户点击保存按钮时,问题就出现了。主要 CRM 联系人将被很好地保存(任何更改都会被检测到并保存),但不会保存任何相关属性 - 无论是新添加的还是修改后的 EF 都会忽略它。

如何强制 Entity Framework 检测到我的相关属性发生了变化?我用它来保存我的主要联系人:

//contact is an instance of CRMContact retrieved from ViewState
if (contact.Id == 0) {
    CRMEntities.CRMContacts.AddObject(contact);
} else {
    CRMContact orig = CRMEntities.CRMContacts.Where(c => c.Id == contact.Id).FirstOrDefault();
    CRMEntities.CRMContacts.ApplyCurrentValues(contact);
}

CRMEntities.SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave);

这适用于联系人实体,但不适用于我的相关实体。要添加和/或更新电话号码和电子邮件,我需要添加什么?

请注意,我不想使用基于代理的更改跟踪。

谢谢

最佳答案

经过大量试验和错误后,我设法将一些有用的东西放在一起。请注意,我不知道这是否是正确的做法。这是来自项目不同部分的代码。 IAHeader 是主要实体,IAAttachmentIAComment 都通过外键链接到 header :

public static void Save(IAHeader head) {
    IAHeader orig = new IAHeader();
    if (head.Id == 0) {
        IAData.Entities.IAHeaders.AddObject(head);
    } else {
        orig = IAData.Entities.IAHeaders.Where(h => h.Id == head.Id).FirstOrDefault();
        IAData.Entities.IAHeaders.ApplyCurrentValues(head);

        foreach (IAComment comm in head.Comments.ToList()) {
            if (comm.Id == 0) {
                comm.IAHeader = null; //disassociate this entity from the parent, otherwise parent will be re-added
                comm.IAId = head.Id;
                IAData.Entities.IAComments.AddObject(comm);
            } else {
                IAComment origComm = orig.Comments.Where(c => c.Id == comm.Id).First();
                IAData.Entities.IAComments.ApplyCurrentValues(comm);
            }
        }

        foreach (IAAttachment att in head.Attachments.ToList()) {
            if (att.Id == 0) {
                att.IAHeader = null; //disassociate this entity from the parent, otherwise parent will be re-added
                att.IAId = head.Id;
                IAData.Entities.IAAttachments.AddObject(att);
            } else {
                IAAttachment origAtt = orig.Attachments.Where(a => a.Id == att.Id).First();
                IAData.Entities.IAAttachments.ApplyCurrentValues(att);
            }
        }
    }
    IAData.Entities.SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave);
}

显然,可以进行很多改进,但这是我到目前为止提出的适用于我的场景的方法。最让我感到困惑的重要部分是必须取消我的导航属性与我的主要实体的关联,否则我会收到“实体 key 已存在”错误或主要实体将被保存两次。

关于c# - 具有快照更改检测的 Entity Framework 中的 POCO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2373664/

相关文章:

c# - 通过依赖注入(inject)在主窗体中初始化接口(interface)

c# - 如何使用 C# 在 tfs 中创建具有本地文件夹映射的工作区

c# - 在 Visual Studio 2012 RC 中看不到 Oracle Data Provider for .NET

c# - Entity Framework 创建的主键不会自动递增

c# - 对 ASP.NET 身份框架和 EF 代码优先的持久性无知

c# - 如何: Get text index from label by mouse position? C#

c# - 事件和委托(delegate),如何将其编码为2级深度?

c# - WCF + EF 4.1 预加载和序列化问题

Azure,DocumentDB无法将POCO对象保存为文档

c# - N 层存储库 POCO - 聚合?