【发布时间】:2010-12-02 21:47:38
【问题描述】:
插入/更新实体时,我需要记录所有已更改的属性。 让我们拿两张表客户和地址。一个客户可以有多个地址。
任务:
将所有已更改的属性写入审计表?
如果你愿意,写一个更新方法的方法是什么。
我看到您可以使用以下内容:
ObjectStateEntry entry = ObjectStateManager.GetObjectStateEntry(entity);
var changes= entry.GetModifiedProperties().
虽然以下是一半的尝试,但不确定您实际上是如何编写该方法的: 你能给我一些指点或帮我写代码吗?
private bool UpdateCustomer(Customer modifiedCustomerDto)
{
using (var ctx = new MyContext())
{
var oldCustomer = ctx.Customers.Where(xx => xx.CustomerId == modifiedCustomerDto.id).Single();
oldCustomer.Name = modifiedCustomerDto.Name;
oldCustomer.Surname = modifiedCustomerDto.Surname;
foreach (var oldAddress in oldCustomer.Addresses)
{
//if it's a new Address add it
//else updateit
//Write to the audit table all the properties that have changed.
}
//Get Modified properties and write to the auditlog
ctx.SaveChanges();
}
}
【问题讨论】:
标签: entity-framework entity-framework-4