具有快照更改检测的实体框架中的POCO

时间:2010-03-03 17:54:44

标签: c# entity-framework poco

我有一组Visual Studio 2010生成的POCO类(刚开始使用从DB Schema生成的POCO模板)。对于给定的用例,我让用户加载一个实体(CRM联系人)并对其进行操作 - 添加电话号码(本身是一个与外键相关的独立实体)和地址(也是一个单独的实体)等。回发我将修改后的实体存储在ViewState中(我不想立即将更改保存到数据库中)。当用户点击“保存”按钮时出现问题。主要的CRM Contact将保存正常(检测并保存任何更改),但不会保存任何相关属性 - 无论是新添加还是修改后的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);

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

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

由于

2 个答案:

答案 0 :(得分:2)

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

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);
}
显然,可以做出很多改进,但这是我到目前为止提出的适用于我的场景的内容。最重要的部分是让我搞砸了我的导航属性与我的主要实体取消关联,否则我会得到“实体密钥已经存在”错误或者主实体会被保存两次。

答案 1 :(得分:0)

我没有看到你改变任何相关值的位置。 docs for ObjectContext.ApplyCurrentValues说:

  

将提供的对象中的标量值复制到ObjectContext中具有相同键的对象中。

(强调补充。)

由于你没有做任何其他改动,我认为没有什么可以检测的。