[英]Multiplicity constraint violation EF Code-First
i've a problem with using Entity-Framework 6. I've two classes 我在使用实体框架6时遇到问题。我有两个类
public class LinkableState : Entity
{
[Required]
public long LinkableId { get; set; }
[ForeignKey(@"LinkableId")]
public virtual Linkable Linkable { get; set; }
public long Counter { get; set; }
public string Debug { get; set; }
}
and 和
public class ProcessInstance : InstanceBase<Process>
{
[Required]
public DateTime InstanceStartTime { get; set; }
public DateTime? InstanceEndTime { get; set; }
public ProcessInstanceState InstanceEndState { get; set; } = ProcessInstanceState.None;
public virtual ICollection<LinkableState> LinkableStates { get; set; } = new List<LinkableState>();
public virtual ICollection<LinkableState> LinkableStateTopCounter { get; set; } = new List<LinkableState>();
}
Within my code i like to change ProcessInstance.LinkableStates like in this example (at this time the elements of reachedStates are detached) 在我的代码中,我喜欢更改此示例中的ProcessInstance.LinkableStates(此时,reachStates的元素是分离的)
using (IDataLayer db = StorageFactory.GetStore())
{
ProcessInstance ix = db.GetProcessInstance(processInstanceId);
ix.LinkableStates = reachedStates;
}
if the db obejct get's disposed and saved i get the error 如果数据库对象被处置并保存,则会出现错误
Multiplicity constraint violated. 违反多重性约束。 The role 'ProcessInstance_LinkableStates_Source' of the relationship 'DataLayer.Mssql.DataBase.ProcessInstance_LinkableStates' has multiplicity 1 or 0..1. 关系“ DataLayer.Mssql.DataBase.ProcessInstance_LinkableStates”的角色“ ProcessInstance_LinkableStates_Source”的多重性为1或0.1。
I don't know why EF thinks that there is more than 1 relation ? 我不知道为什么EF认为存在不止一种关系? I've checked the EDMX file (reverse from Code-First) which looks like this: 我已经检查了EDMX文件(与Code-First相反),如下所示:
As you can see in the db-entries the key of ProcessInstance_Id (ProcessInstance.LinkableStates) has been nulled even if ID {1, 3} should be set with Process.Id = 1. 如您在数据库条目中看到的,即使ID {1,3}应该设置为Process.Id = 1,ProcessInstance_Id的键(ProcessInstance.LinkableStates)也已为空。
This error occured while i switched from 我从切换时发生了此错误
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
to 至
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
At the Moment i've absolute no idea why this error occurs when i switch from lazy to eager loading. 当下,我绝对不知道为什么当我从延迟加载切换到渴望加载时会发生此错误。 i already tried to clear the list and add the new elements manually like: 我已经尝试清除列表并手动添加新元素,例如:
using (IDataLayer db = StorageFactory.GetStore())
{
ProcessInstance ix = db.GetProcessInstance(processInstanceId);
ix.LinkableStates.Clear();
foreach (LinkableState e in unvisitedStates)
{
ix.LinkableStates.Add(e);
}
}
but this does not change anything. 但这并没有改变任何东西。
Do you have any ideas how i can fix this problem ? 你有什么想法我可以解决这个问题吗?
thanks in advance :) 提前致谢 :)
Okay, seems like I've solved the Problem. 好的,看来我已经解决了问题。
I did two things. 我做了两件事。 First, i've added the InverseProperty Annotation to my classes 首先,我在类中添加了InverseProperty Annotation
public class LinkableState : Entity
{
...
public long? LinkableStatesId { get; set; }
public long? LinkableStateTopCounterId { get; set; }
[InverseProperty(@"LinkableStates")]
[ForeignKey(@"LinkableStatesId")]
public virtual ProcessInstance ProcessInstanceLinkableStates { get; set; }
[InverseProperty(@"LinkableStateTopCounter")]
[ForeignKey(@"LinkableStateTopCounterId")]
public virtual ProcessInstance ProcessInstanceLinkableStateTopCounter { get; set; }
}
public class ProcessInstance : InstanceBase<Process>
{
...
[InverseProperty(@"ProcessInstanceLinkableStates")]
public virtual ICollection<LinkableState> LinkableStates { get; set; } = new List<LinkableState>();
[InverseProperty(@"ProcessInstanceLinkableStateTopCounter")]
public virtual ICollection<LinkableState> LinkableStateTopCounter { get; set; } = new List<LinkableState>();
}
and the second thing is that i set the reference Id for LinkableStates by Hand before i save them and check for a already stored version first. 第二件事是我在保存它们之前先手动设置LinkableStates的引用ID,然后首先检查已存储的版本。
foreach (LinkableState e in copy)
{
LinkableState tmp = db.GetLinkableState(e.Id) ?? e;
tmp.LinkableStatesId = ix.Id;
ix.LinkableStates.Add(tmp);
}
for now this solved my "Multiplicity constraint violation" error. 现在,这解决了我的“违反多重约束”错误。 Hope this will help someone else faceing this problem. 希望这可以帮助其他面临此问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.