违反了多重约束(在 XAF 中,当 createdObject.ObjectSpace.ModifiedObjects.Count 尚未被访问时。)

Multiplicity constraint violated (In XAF When createdObject.ObjectSpace.ModifiedObjects.Count has not been accessed.)

问题总结:

为什么 "Multiplicity constraint violated" 可以通过引入以下代码行来解决?

var numModifiedObjects =createdObject.ObjectSpace.ModifiedObjects.Count;

是否有一个设置可以使这些工作变得不必要?

问题背景及解决方法:

在我的 winforms xaf、EF6.2 Code First 项目中,我有以下自引用的业务对象。

[NavigationItem("Main")]
public class H2Category : IHCategory 
{
    public H2Category()
    {
        Children = new BindingList<H2Category>();
    }
    [Browsable(false)]
    public Int32 ID { get; protected set; }
    public String Name { get; set; }

    public H2Category Parent { get; set; }

    public virtual IList<H2Category> Children { get; set; }

    [NotMapped, Browsable(false), RuleFromBoolProperty("H2CategoryCircularReferences", DefaultContexts.Save, "Circular refrerence detected. To correct this error, set the Parent property to another value.", UsedProperties = "Parent")]
    public Boolean IsValid
    {
        get
        {
            H2Category currentObj = Parent;
            while (currentObj != null)
            {
                if (currentObj == this)
                {
                    return false;
                }
                currentObj = currentObj.Parent;
            }
            return true;
        }
    }
    IBindingList ITreeNode.Children => Children as IBindingList;

    ITreeNode IHCategory.Parent
    {
        get => Parent as IHCategory;
        set => Parent = value as H2Category;
    }
    ITreeNode ITreeNode.Parent => Parent as ITreeNode;
}

并且在我使用的 dbContext OnModelCreating 中

   modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

   modelBuilder.Entity<H2Category>().HasMany(x => x.Children).WithOptional(x => x.Parent);

Parent_Id);

数据库在 SQL 服务器对象资源管理器中看起来是正确的

而且数据看起来是正确的。

在XAF Treeview中,当用户添加子记录时,我希望将新子记录的父项设置为当前记录。

为此,我使用了 NewObjectViewController

public partial class H2CategoryController : ViewController
{
    private NewObjectViewController controller;
    public H2CategoryController()
    {
        InitializeComponent();
        TargetObjectType = typeof(H2Category);
    }
    protected override void OnActivated()
    {
        controller = Frame.GetController<NewObjectViewController>();
        controller.ObjectCreated += controller_ObjectCreated;
        base.OnActivated();
    }
    private void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e)
    {
        var createdObject = e.CreatedObject as H2Category;
        var currentObject = View.CurrentObject as H2Category;
        // inspection here shows that currentObject and createdObject are different but they have the same proxy
        var s = $"created object Id is {createdObject.ID}  current object id is {currentObject.ID}";
        Console.WriteLine(s);

        var propertyCollectionSource = (View as ListView)?.CollectionSource as PropertyCollectionSource;
        if (!(propertyCollectionSource?.MasterObject is H2Category master)) return;
        createdObject.Parent = master;
        createdObject.Name = "t";
        master.Children.Add(createdObject);

        Console.WriteLine(master.ID);

    }

    protected override void OnDeactivated()
    {
        controller.ObjectCreated += controller_ObjectCreated;
        base.OnDeactivated();
    }
}

一旦用户开始输入新记录,就会出现以下错误

    Multiplicity constraint violated. The role 'H2Category_Children_Source' of the relationship 'SBD.GL.Module.BusinessObjects.H2Category_Children' has multiplicity 1 or 0..1.

    at System.Data.Entity.Core.Objects.EntityEntry.AddDetectedRelationship[T](Dictionary`2 relationships, T relatedObject, RelatedEnd relatedEnd)
    at System.Data.Entity.Core.Objects.EntityEntry.AddRelationshipDetectedByGraph(Dictionary`2 relationships, Object relatedObject, RelatedEnd relatedEndFrom, Boolean verifyForAdd)
    at System.Data.Entity.Core.Objects.EntityEntry.DetectChangesInRelationshipsOfSingleEntity()
   at System.Data.Entity.Core.Objects.ObjectStateManager.DetectChangesInNavigationProperties(IList`1 entries)
   at System.Data.Entity.Core.Objects.ObjectStateManager.DetectChanges()
   at System.Data.Entity.Core.Objects.ObjectContext.DetectChanges()
   at DevExpress.ExpressApp.EF.EFObjectSpace.DetectChanges()
   at DevExpress.ExpressApp.EF.EFObjectSpace.GetModifiedObjects()
   at DevExpress.ExpressApp.EF.EFObjectSpace.get_ModifiedObjects()
   at DevExpress.ExpressApp.Win.SystemModule.CustomCollectModifiedObjectsEventArgs.CollectModifiedObjects(Boolean checkObjectSpaceIsModified)
   at DevExpress.ExpressApp.Win.SystemModule.LockController.GetModifiedObjects()
   at DevExpress.ExpressApp.Win.SystemModule.LockController.CheckLocking(LockController controller)
   at DevExpress.ExpressApp.Win.SystemModule.LockController.CheckLocking(Object obj)
   at DevExpress.ExpressApp.Win.SystemModule.LockController.ViewObjectSpace_ObjectChanged(Object sender, ObjectChangedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at DevExpress.ExpressApp.BaseObjectSpace.OnObjectChanged(ObjectChangedEventArgs args)
   at DevExpress.ExpressApp.EF.EFObjectSpace.SetModified(Object obj, ObjectChangedEventArgs args)
   at DevExpress.ExpressApp.BaseObjectSpace.SetModified(Object obj, IMemberInfo memberInfo)
   at DevExpress.ExpressApp.DetailView.Editor_ControlValueChanged(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at DevExpress.ExpressApp.Editors.PropertyEditor.OnControlValueChanged()
   at DevExpress.ExpressApp.Win.Editors.DXPropertyEditor.Editor_EditValueChanged(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at DevExpress.XtraEditors.Repository.RepositoryItem.RaiseEditValueChangedCore(EventArgs e)
   at DevExpress.XtraEditors.Repository.RepositoryItem.RaiseEditValueChanged(EventArgs e)
   at DevExpress.XtraEditors.BaseEdit.RaiseEditValueChanged()
   at DevExpress.XtraEditors.BaseEdit.OnEditValueChanged()
   at DevExpress.XtraEditors.TextEdit.OnEditValueChanged()
   at DevExpress.XtraEditors.BaseEdit.OnEditValueChanging(ChangingEventArgs e)
   at DevExpress.XtraEditors.TextEdit.OnEditValueChanging(ChangingEventArgs e)
   at DevExpress.XtraEditors.BaseEdit.set_EditValue(Object value)
   at DevExpress.XtraEditors.TextEdit.OnMaskBox_ValueChanged(Object sender, EventArgs e)
   at DevExpress.XtraEditors.Mask.MaskBox.RaiseEditTextChanged()
   at DevExpress.XtraEditors.Mask.MaskBox.MaskStrategy.SimpleStrategy.DoAfterTextChanged(EventArgs e)
   at DevExpress.XtraEditors.Mask.MaskBox.OnTextChanged(EventArgs e)
   at System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m)
   at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
   at System.Windows.Forms.TextBox.WndProc(Message& m)
   at DevExpress.XtraEditors.Mask.MaskBox.BaseWndProc(Message& m)
   at DevExpress.XtraEditors.Mask.MaskBox.MaskStrategy.SimpleStrategy.DoWndProc(Message& m)
   at DevExpress.XtraEditors.Mask.MaskBox.WndProc(Message& m)
   at DevExpress.XtraEditors.TextBoxMaskBox.WndProc(Message& msg)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我注意到当我设置父级时,调试值看起来是正确的

但我很困惑,所有的 H2Category 对象都具有相同的代理值。

我问过 issue at Dev Express 但在这里问也是 Entity Framework 相关的。

[更新 - 变通]

我向业务对象添加了 IObjectSpaceLink 接口。这将 ObjectSpace 公开为业务对象的 属性。

当我中断 controller_ObjectCreated 方法时,我能够看到 ModifiedObjects 属性 显示错误

我发现,如果我将 createdObject.ObjectSpace.ModifiedObjects.Count 赋值给一个变量,那么问题就不会发生。

       private void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e)
    {
        var createdObject = e.CreatedObject as H2Category;
        var numModifiedObjects =createdObject.ObjectSpace.ModifiedObjects.Count;

        var currentObject = View.CurrentObject as H2Category;
        var s = $"created object Id is {createdObject.ID}  current object id is {currentObject.ID}";

        var propertyCollectionSource = (View as ListView)?.CollectionSource as PropertyCollectionSource;
        if (!(propertyCollectionSource?.MasterObject is H2Category master)) return;

        var m = e.ObjectSpace.GetObject(master);

        createdObject.Parent = m;
        createdObject.Name = "t";
        m.Children.Add(createdObject);
        numModifiedObjects = createdObject.ObjectSpace.ModifiedObjects.Count;
        Console.WriteLine(master.ID);

    }

我尝试禁用代理创建,但没有帮助。

来自Dev Express support answer.

由于我的 H2Category class 中的错误,该关系在我的项目中无法正常工作。 Parent 属性 必须声明为虚拟的。

此外,我的 ICategoryController 与内置的 TreeNodeController 冲突。该控制器还订阅了 NewObjectViewController.ObjectCreated 事件和 link 一个新创建的 ITreeNode 对象,其父节点在源列表视图中被选中。默认情况下,保存更改时会 link 编辑对象。要在创建后立即 link 新对象,请将 NewObjectViewController.LinkNewObjectToParentImmediately 属性 设置为 true