多重约束违规调试

时间:2011-05-03 11:34:19

标签: c#-4.0 entity-framework-4

我的实体模型中出现了多重约束违规。

在我的实体模型上,我有两个关系属性:

  1. SubstanceTypeMixConstituents
  2. 分类
  3. SubstanceTypeMixConstituents
      - 多重性*(许多)
    分类
      - 多重性:1(一)
      - 外键,非空

    enter image description here

    如何找到导致问题的原因并解决此问题?

    System.InvalidOperationException: A relationship multiplicity constraint violation occurred: An EntityReference expected at least one related object, but the query returned no related objects from the data store.
       at System.Data.Objects.DataClasses.EntityReference`1.Load(MergeOption mergeOption)
       at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
       at System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)
       at System.Data.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__2(TProxy proxy, TItem item)
       at System.Data.Entity.DynamicProxies.SubstanceType_BEE32ACA75386E981F7CA3F6A3C565BC1D8ADACA228C603A2EACC918DCDCBA30.get_Category()
    

1 个答案:

答案 0 :(得分:2)

据我了解,您有两个实体 - CategorySubstanceTypeMixConstituentOne-to-many关系 - Category可以有多个SubstanceTypeMixConstituents,但是SubstanceTypeMixConstituent可以(并且必须)只有一个Category(如果我错了,请纠正我)。您获得的错误消息意味着它所说的内容 - 您要么尝试使用空集Category保存SubstanceTypeMixConstituents,要么保存SubstanceTypeMixConstituent而不保存Category。< / p>