违反多重约束 EF Code-First
Multiplicity constraint violation EF Code-First
我在使用 Entity-Framework 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; }
}
和
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>();
}
在我的代码中,我想像这个例子中那样更改 ProcessInstance.LinkableStates(此时 reachedStates 的元素是分离的)
using (IDataLayer db = StorageFactory.GetStore())
{
ProcessInstance ix = db.GetProcessInstance(processInstanceId);
ix.LinkableStates = reachedStates;
}
如果 db obejct 被处理并保存我得到错误
Multiplicity constraint violated. The role
'ProcessInstance_LinkableStates_Source' of the relationship
'DataLayer.Mssql.DataBase.ProcessInstance_LinkableStates' has
multiplicity 1 or 0..1.
我不知道为什么 EF 认为存在不止 1 个关系?我已经检查了 EDMX 文件(与 Code-First 相反),它看起来像这样:
正如您在 db-entries 中看到的那样,即使 ID {1, 3} 应设置为 Process.Id =,ProcessInstance_Id (ProcessInstance.LinkableStates) 的键也已为空1.
我从
切换时发生此错误
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
至
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
目前我完全不知道为什么当我从惰性加载切换到急切加载时会出现此错误。我已经尝试清除列表并手动添加新元素,如:
using (IDataLayer db = StorageFactory.GetStore())
{
ProcessInstance ix = db.GetProcessInstance(processInstanceId);
ix.LinkableStates.Clear();
foreach (LinkableState e in unvisitedStates)
{
ix.LinkableStates.Add(e);
}
}
但这并没有改变任何东西。
你有什么办法可以解决这个问题吗?
提前致谢:)
好的,看来我已经解决了问题。
我做了两件事。首先,我将 InverseProperty 注释添加到我的 类
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>();
}
第二件事是我在保存它们之前手动设置了 LinkableStates 的引用 ID,并首先检查已存储的版本。
foreach (LinkableState e in copy)
{
LinkableState tmp = db.GetLinkableState(e.Id) ?? e;
tmp.LinkableStatesId = ix.Id;
ix.LinkableStates.Add(tmp);
}
现在这解决了我的 "Multiplicity constraint violation" 错误。
希望这会帮助其他人面对这个问题。
我在使用 Entity-Framework 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; }
}
和
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>();
}
在我的代码中,我想像这个例子中那样更改 ProcessInstance.LinkableStates(此时 reachedStates 的元素是分离的)
using (IDataLayer db = StorageFactory.GetStore())
{
ProcessInstance ix = db.GetProcessInstance(processInstanceId);
ix.LinkableStates = reachedStates;
}
如果 db obejct 被处理并保存我得到错误
Multiplicity constraint violated. The role 'ProcessInstance_LinkableStates_Source' of the relationship 'DataLayer.Mssql.DataBase.ProcessInstance_LinkableStates' has multiplicity 1 or 0..1.
我不知道为什么 EF 认为存在不止 1 个关系?我已经检查了 EDMX 文件(与 Code-First 相反),它看起来像这样:
正如您在 db-entries 中看到的那样,即使 ID {1, 3} 应设置为 Process.Id =,ProcessInstance_Id (ProcessInstance.LinkableStates) 的键也已为空1.
我从
切换时发生此错误Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
至
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
目前我完全不知道为什么当我从惰性加载切换到急切加载时会出现此错误。我已经尝试清除列表并手动添加新元素,如:
using (IDataLayer db = StorageFactory.GetStore())
{
ProcessInstance ix = db.GetProcessInstance(processInstanceId);
ix.LinkableStates.Clear();
foreach (LinkableState e in unvisitedStates)
{
ix.LinkableStates.Add(e);
}
}
但这并没有改变任何东西。
你有什么办法可以解决这个问题吗?
提前致谢:)
好的,看来我已经解决了问题。
我做了两件事。首先,我将 InverseProperty 注释添加到我的 类
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>();
}
第二件事是我在保存它们之前手动设置了 LinkableStates 的引用 ID,并首先检查已存储的版本。
foreach (LinkableState e in copy)
{
LinkableState tmp = db.GetLinkableState(e.Id) ?? e;
tmp.LinkableStatesId = ix.Id;
ix.LinkableStates.Add(tmp);
}
现在这解决了我的 "Multiplicity constraint violation" 错误。 希望这会帮助其他人面对这个问题。