- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题总结:
为什么可以通过引入以下代码行来解决“违反多重性约束”的问题?
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 对象资源管理器中看起来是正确的
而且数据看起来是正确的。
在 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 接口(interface)。这将 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
类中的错误,该关系在我的项目中无法正常工作。 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/
问题总结: 为什么可以通过引入以下代码行来解决“违反多重性约束”的问题? var numModifiedObjects =createdObject.ObjectSpace.ModifiedObjec
我是一名优秀的程序员,十分优秀!