防止导航属性插入 [英] prevent navigation property insertion
问题描述
但是当我插入类型B的实体时,它会自动插入连接的A实体,而不需要额外的检查和更新。
我该如何解决? / p>
更新
我决定使用这个答案建议。但是在OnBeforeInsert()中,我可能会在上下文中添加一个新的实体,因为在 var changedEntities = ChangeTracker.Entries();
我该如何解决?
EF有很少的扩展点。所以有时很难自定义。
这个答案是我的以前的答案
public抽象类Entity
/ pre>
{
public virtual void OnBeforeInsert(){}
public virtual void OnBeforeUpdate(){}
}
public class Category:Entity
{
public string Name {get;组; }
public string UrlName {get;组;
public override void OnBeforeInsert()
{
// ur logic
}
}
然后在您的
DbContext
类订阅ObjectStateManagerChanged
事件ObjectStateManager
。public class MyContext:DbContext
{
public override int SaveChanges()
{
//拦截实体更改
UnderlyingObjectContext.ObjectStateManager.ObjectStateManagerChanged
+ = OnObjectStateManagerChanged;
var changedEntities = ChangeTracker.Entries();
foreach(var changedEntity in changedEntities)
{
if(changedEntity.Entity是Entity)
{
var entity =(Entity)changedEntity.Entity ;
switch(changedEntity.State)
{
case EntityState.Added:
entity.OnBeforeInsert();
break;
case EntityState.Modified:
entity.OnBeforeUpdate();
break;
}
}
}
return base.SaveChanges();
}
ObjectContext UnderlyingObjectContext
{
get
{
return((IObjectContextAdapter)this).ObjectContext;
}
}
void OnObjectStateManagerChanged(object sender,CollectionChangeEventArgs e)
{
if(e.Action == CollectionChangeAction.Add)
{
//不是所有添加的实体都是新的
if(UnderlyingObjectContext.ObjectStateManager
.GetObjectStateEntry(e.Element).State == EntityState.Added)
{
if(e.Element is Entity)
{
((Entity)e.Element).OnBeforeInsert();
}
}
}
}
}
如果您使用EF 4.0,则需要相应地进行自定义。
I have an entity A that used as navigation property for another entity B. I would like to control the Insertion for entity A. Thats meen that whenever I would insert A I have to perform other checks and update other entities.
But when I Insert entities of type B it automatically inserts the connected A entities without the extra checks and updates I need.How can I solve this?
UPDATE I decided to use this answer as suggested. But in the OnBeforeInsert() I might add a new entities to the context which their OnBeforeInsert() won't be called since in the time
var changedEntities = ChangeTracker.Entries();
was called the new entitied wasn't exist yet.
How can I solve this?解决方案EF has very few extension points. So it is sometimes very difficult to customize.
This answer is an extension of my previous answer
public abstract class Entity { public virtual void OnBeforeInsert(){} public virtual void OnBeforeUpdate(){} } public class Category : Entity { public string Name { get; set; } public string UrlName{ get; set; } public override void OnBeforeInsert() { //ur logic } }
Then in your
DbContext
class subscribe toObjectStateManagerChanged
event ofObjectStateManager
.public class MyContext : DbContext { public override int SaveChanges() { //intercept entity changes UnderlyingObjectContext.ObjectStateManager.ObjectStateManagerChanged += OnObjectStateManagerChanged; var changedEntities = ChangeTracker.Entries(); foreach (var changedEntity in changedEntities) { if (changedEntity.Entity is Entity) { var entity = (Entity)changedEntity.Entity; switch (changedEntity.State) { case EntityState.Added: entity.OnBeforeInsert(); break; case EntityState.Modified: entity.OnBeforeUpdate(); break; } } } return base.SaveChanges(); } ObjectContext UnderlyingObjectContext { get { return ((IObjectContextAdapter)this).ObjectContext; } } void OnObjectStateManagerChanged(object sender, CollectionChangeEventArgs e) { if (e.Action == CollectionChangeAction.Add) { //not all added entities are new if (UnderlyingObjectContext.ObjectStateManager .GetObjectStateEntry(e.Element).State == EntityState.Added) { if (e.Element is Entity) { ((Entity)e.Element).OnBeforeInsert(); } } } } }
If you are using EF 4.0 you will need to customize this accordingly.
这篇关于防止导航属性插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!