gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:26:28 26 4
gpt4 key购买 nike

问题总结:

为什么可以通过引入以下代码行来解决“违反多重性约束”的问题?

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 Server 对象资源管理器中看起来是正确的 the SQL Server Object Explorer

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

data table

在 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)

error message

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

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

我已经询问过 issue at Dev Express,但在这里询问,因为它与 Entity Framework 相关。

[更新 - 变通]

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

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

ModifiedObjects error

我发现,如果我将 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);

}

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

exception

最佳答案

来自 Dev Express support answer

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

此外,我的 ICategoryController 与内置的 TreeNodeController 冲突。该 Controller 还订阅了 NewObjectViewController.ObjectCreated 事件,并将新创建的 ITreeNode 对象与在源 ListView 中选择的父节点链接起来。默认情况下,保存更改时链接对象。要在创建后立即链接新对象,请将 NewObjectViewController.LinkNewObjectToParentImmediately 属性设置为 true

关于entity-framework-6 - 违反了多重性约束(在 XAF 中,当 createdObject.ObjectSpace.ModifiedObjects.Count 尚未被访问时。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54079037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com