如何确定 EF 4.1 中具有引用属性的实体是否脏?
在启用自动更改跟踪(代理对象)的 Entity Framework 4.1 中,支持 Entity.IsDirty 似乎应该很容易。然而,到目前为止我还没有发现这种情况。
public class DomainContext : DbContext, IDomainContext
{
/// <summary>
/// Indicates whether changes have been made to the entity with that have
/// not been saved. This method assumes EF Proxies are being used for change tracking.
/// </summary>
/// <param name="entityId">The Id of the entity.</param>
/// <returns>True if the entity is dirty, false otherwise.</returns>
public bool HasChanges(Guid entityId)
{
foreach (DbEntityEntry entry in this.ChangeTracker.Entries())
{
IEntity entity = entry.Entity as IEntity;
if (entity != null)
{
if (entity.Id == entityId && entry.State != System.Data.EntityState.Unchanged)
{
return true;
}
}
}
return false;
}
}
当对实体上的引用/复杂属性的属性进行更改时,上述方法似乎不起作用。例如:
public class UserPreferences : EntityBase
{
/// <summary>
/// Comma delimited list of AFEs for which the user is interested in viewing Events.
/// </summary>
public virtual string ViewableAFEs { get; set; }
/// <summary>
/// The primary operating area for the user. This is used for defaulting purposes.
/// </summary>
public virtual OperatingArea PrimaryOperatingArea
{
get { return _primaryOpArea; }
set
{
if (_primaryOpArea != value)
{
_primaryOpArea = value;
RaisePropertyChanged(() => PrimaryOperatingArea);
}
}
}
}
如果我新建上面的类,或者从数据库获取现有的 UserPreferences 实体,然后更改 PrimaryOperatingArea 属性,DomainContext.HasChanges 将返回 false。我相信发生这种情况是因为实体框架跟踪复杂的&引用属性与值类型属性不同。
当调用上面的 HasChanges 方法时,更改 ViewableAFEs 属性(字符串)确实会显示为更改。
我的问题是如何在派生的 DomainContext 上公开一个通用方法,该方法将评估所有属性(包括复杂、引用、集合类型)并确定实体是否脏?
我很好奇,你们中的其他人是否使用 EF 4.1 利用 EF 来实现 IsDirty 功能,或者您是否通过使用 INotifyPropertyChanged 等来推出自己的 isDirty?
谢谢!
It seems like supporting Entity.IsDirty should be easy with Entity Framework 4.1 with Automatic Change Tracking (proxy objects) turned on. However, I have not found this to be the case so far.
public class DomainContext : DbContext, IDomainContext
{
/// <summary>
/// Indicates whether changes have been made to the entity with that have
/// not been saved. This method assumes EF Proxies are being used for change tracking.
/// </summary>
/// <param name="entityId">The Id of the entity.</param>
/// <returns>True if the entity is dirty, false otherwise.</returns>
public bool HasChanges(Guid entityId)
{
foreach (DbEntityEntry entry in this.ChangeTracker.Entries())
{
IEntity entity = entry.Entity as IEntity;
if (entity != null)
{
if (entity.Id == entityId && entry.State != System.Data.EntityState.Unchanged)
{
return true;
}
}
}
return false;
}
}
The above method doesn't seem to work when changes have been made to properties on the entity that are reference/complex properties. For instance:
public class UserPreferences : EntityBase
{
/// <summary>
/// Comma delimited list of AFEs for which the user is interested in viewing Events.
/// </summary>
public virtual string ViewableAFEs { get; set; }
/// <summary>
/// The primary operating area for the user. This is used for defaulting purposes.
/// </summary>
public virtual OperatingArea PrimaryOperatingArea
{
get { return _primaryOpArea; }
set
{
if (_primaryOpArea != value)
{
_primaryOpArea = value;
RaisePropertyChanged(() => PrimaryOperatingArea);
}
}
}
}
If I new up the above class, or get an existing UserPreferences entity from the database and then change the PrimaryOperatingArea property, DomainContext.HasChanges will return false. I believe this is occuring because Entity Framework tracks complex & reference properties differently from value type properties.
Changing the ViewableAFEs property (string) does show up as a change when calling the HasChanges method above.
My question is how do I expose a generic method on my derived DomainContext that will evaluate all properties (including complex, reference, collection types) and determine if the entity is dirty?
I'm curious, have others of you using EF 4.1 leveraged EF for IsDirty functionality, or did you roll your own isDirty by using INotifyPropertyChanged, etc. ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DbContext API 已经公开了用于检查实体或某些属性是否已更改的选项。
检查实体是否被修改:
var myFooHasChanges = context.Entry(myFoo).State == EntityState.Modified;
检查属性是否被修改:
var barWasModified = context.Entry(myFoo).Property(u => u.Bar).IsModified;
您可以在以下位置阅读所有相关内容:在 EF 4.1 中使用 DbContext 第 5 部分:使用属性值
DbContext API already exposes options to check if an entity or some properties were changed.
to check if an entity was modified:
var myFooHasChanges = context.Entry(myFoo).State == EntityState.Modified;
to check if a property was modified:
var barWasModified = context.Entry(myFoo).Property(u => u.Bar).IsModified;
You can read all about this in: Using DbContext in EF 4.1 Part 5: Working with Property Values