代码之家  ›  专栏  ›  技术社区  ›  Atzoya

数据上下文的SubmitChanges方法导致实体引用设置为null

  •  6
  • Atzoya  · 技术社区  · 14 年前

    MotherClass看起来像这样:

    public class MotherClass
    {
         private EntitySet<ChildClass> _Children;
    
         [Column]
         public int Id { get; set; }
    
         [Association(Storage = "_Children", ThisKey = "Id", OtherKey = "MotherId")]
         public EntitySet<ChildClass> Children
         {
               get { return _Children; }
               set { _Children= value; }
         }
    }
    

    ChildClass看起来像:

    public class ChildClass
    {
         private EntityRef<MotherClass> _Mother;
         private EntitySet<ChildChildrenClass> _ChildChildren;
    
         [Column]
         public int Id { get; set; }
    
         [Column]
         public int MotherId { get; set; }
    
         [Association(Storage = "_Mother", IsForeignKey = true, ThisKey = "MotherId", OtherKey = "Id")]
         public MotherClass Mother
         {
               get { return _Mother.Entity; }
               set { _Mother.Entity = value; }
         }
    
         [Association(Storage = "_ChildChildren", ThisKey = "Id", OtherKey = "ChildId", DeleteRule = "NO ACTION")]
         public EntitySet<ChildChildrenClass> ChildChildren
         {
               get { return _ChildChildren; }
               set { _ChildChildren= value; }
         }
    }
    

    第三种职业被神奇地命名为childrenclass:

    public class ChildChildrenClass
        {
             private EntityRef<ChildClass> _Child;
    
             [Column]
             public int Id { get; set; }
    
             [Column]
             public int ChildId { get; set; }
    
             [Association(Storage = "_Child", IsForeignKey = true, ThisKey = "ChildId", OtherKey = "Id")]
             public ChildClass Child
             {
                   get { return _Child.Entity; }
                   set { _Child.Entity = value; }
             }
        }
    

    当我们对ChildClass对象进行更新并删除与之关联的一些childrenClass项时,问题就出现了是的。是的代码如下所示:

    DataContext dc = new DataContext(conStr);
    dc.StartTransaction();//our custom method for handling transactions
    ChildClass cclass = dc.ChildClass.GetById(id);//our method for getting the ChildClass from db
    //... here we set some values we want to edit
    //...
    //...
    dc.SubmitChanges(ConflictMode.FailOnFirstConflict);//these actions are cool
    //after this the problems arise
     List<ChildChildrenClass> ccc = GetAllChildren();//method that gets all the childChildrenClass objects from db
    foreach (ChildChildrenClass child in ccc)
    {
         dc.GetTable(child.GetType()).DeleteOnSubmit(child);
    }
    dc.SubmitChanges(ConflictMode.FailOnFirstConflict);
    //AFTER CALLING THIS METHOD THE PROBLEM APPEARS
    

    上面提到的问题是班级。妈妈属性被神奇地设置为null。经过大量调试(在母集方法中放置brakepoints揭示了这一点),我们注意到在一些外部代码中SubmitChanges()期间,属性被设置为null。

    SubmitChanges()方法成功完成(将删除childrenClass项),但这会导致在此之后运行的代码出现问题。我们正在使用相同的DataContext(因为事务),并再次调用引发此异常的SubmitChanges()方法:

    在系统数据.Linq.ChangeTracker.StandardChangeTrackerS标准跟踪对象.SynchDependentData() 在系统数据.Linq.ChangeProcessor.SubmitChanges文件(冲突模式故障模式)

    3 回复  |  直到 14 年前
        1
  •  2
  •   Venemo    14 年前

    我在很久以前写博客引擎的时候也遇到过这个问题。问题是在我从连接表中删除了几行之后出现的。删除的东西去好,然后不管我做了什么,这个确切的例外出现在第二天 SubmitChanges()

    • 创建DataContext的新实例

    我知道这是 非常 哈奇,但这是我唯一能解决的办法。我看到你在那里使用一个事务,这会使这变得有点困难。或许可以尝试使用两个不同的事务(一个用于旧的DataContext,一个用于新的),如果第二个事务失败,则回滚第一个事务?
    我知道这很难听。

    也许可以尝试使用另一个ORM(例如NHibernate),它对此没有问题。

        2
  •  1
  •   Falcon    14 年前

    1. 这是一个很好的工具,但只适用于 简单域。它有它的问题 有关系。
    2. 有很多选择。如果 ADO.NET实体框架。如果你 有一个复杂的域映射尝试 极大的坚持和无知。 亚音速的粉丝也很多 在外面。
        3
  •  1
  •   Neil T.    14 年前

    DataContext 实例应该 从未 可重复使用。每次都是因为框架没有办法保证数据之间没有变化 SubmitChanges() 调用时,建议的方法是在提交更改后处理DataContext,如果另一个事务需要 需要打电话。

    数据上下文 对象已包装事务中的任何插入、更改和/或删除。