防止导航属性插入 [英] prevent navigation property insertion

查看:156
本文介绍了防止导航属性插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体A,用作另一个实体B的导航属性。我想控制实体A的插入。这样,只要我插入AI,必须执行其他检查并更新其他实体。

但是当我插入类型B的实体时,它会自动插入连接的A实体,而不需要额外的检查和更新。



我该如何解决? / p>

更新
我决定使用这个答案建议。但是在OnBeforeInsert()中,我可能会在上下文中添加一个新的实体,因为在 var changedEntities = ChangeTracker.Entries();
我该如何解决?

解决方案

EF有很少的扩展点。所以有时很难自定义。



这个答案是我的以前的答案

  public抽象类Entity 
{
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
}
}
/ pre>

然后在您的 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 to ObjectStateManagerChanged event of ObjectStateManager.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆