问题加载父实体的子实体。单向映射和1到0..1与共享主键的关系? [英] Issue loading child entity of a parent entity. Unidirectional mapping and 1 to 0..1 relationship with a Shared primary Key?

查看:111
本文介绍了问题加载父实体的子实体。单向映射和1到0..1与共享主键的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试加载父实体的子实体时,它将加载默认值。如果我尝试显式加载它抛出异常
多重约束违反。关系CodeFirstNamespace.Association_Customer的角色Association_Customer_Target具有多重性1或0..1。在检索复杂图形的子实体时抛出此异常。

When I try to load child entity of parent entity it loads with default values. If i try to load explicitly it throws exception Multiplicity constraint violated. The role 'Association_Customer_Target' of the relationship 'CodeFirstNamespace.Association_Customer' has multiplicity 1 or 0..1. This exception is thrown while retrieving the child entities of a complex graph.

我有一个图关联,其中有一个子实体客户,其关系为 1到零或一个,并具有独立关联。* 主键 * 共享。我正在使用EF6。懒惰加载启用。

I have a graph Association which has a child entity Customer with a relationship of one to zero or one and has an Independent association.*Primary key* is shared. I'm using EF6. lazy loading is enabled.

public class Association
{
   public virtual long Id { get; set; }
   public virtual string ExternalId { get; set; }
   public virtual int OrganizationId { get; set; }
   public virtual AssociationType AssociationType { get; set; }
   public virtual Customer Customer {get; set;}
   public Association()
   {
       Customer = new Customer();
   }
}

客户类。

public class Customer
{
  public virtual long Id { get; set; } //Shared primary key
  public virtual ICollection<Item> Items {get; set;}
  public virtual ICollection<Complaint> Complaints {get; set;}
  public customer()
  {
    Items = new List<Item>();
    Complaints = new List<Complaint>();
  }
}

映射是单向的:

public class AssociationMapping:EntityTypeConfiguration<Association>
{
   public AssociationMapping() : base()
   {
     HasKey(x => x.Id);
     Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
     Property(x => x.ExternalId).IsRequired();
     Property(x => x.OrganizationId).IsRequired();
     Property(x => x.AssociationType);
     HasOptional(x => x.Customer).WithRequired().WillCascadeOnDelete();
   }
}

public class CustomerMapping : EntityTypeConfiguration<Customer>
{
  public CustomerMapping ():base()
  {
    HasKey(x => x.Id);
    Property(x => x.Id);
    HasMany(x => x.Items)
       .WithOptional()
       .HasForeignKey(key => key.CustomerId)
       .WillCascadeOnDelete();
    HasMany(x => x.Complaints)
      .WithOptional()
      .HasForeignKey(key => key.CustomerId)
      .WillCascadeOnDelete();
  } 
}

加载我的关联实体时,它完全加载,但孩子实体客户被加载默认值,当我尝试加载客户明确地引发异常。

When I Load My association entity it loads perfectly but child entity Customer is loaded with default values when i try to load Customer explicitly it throws the exception.

 var dbassociation = Single<Association>(x => x.OrganizationId== asso.organizationId && x.ExternalId == asso.ExternalId && x.AssociationType == asso.AssociationType);
 dbassociation.Customer = Single<Customer>(x => x.id == dbassociation.id);

[更新:单一方法]

[Update: Single Method]

public TEntity Single<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>>criteria) {
  return Context.Set<TEntity>().SingleOrDefault(criteria);   }

为了测试的目的,我尝试通过删除关联类中的虚拟客户端属性尝试加载,并尝试以下但它抛出相同的优势

For testing purpose I have tried to eager load by removing virtual on Customer property in association class and tried following but it throws same excepetion

Context.Configuration.LazyLoadingEnabled = false;
Context.Entry<Association>(dbassociation).Reference<Customer>(pa => pa.Customer).Load();

我也尝试过同样的异常

var dbassociation = Context.Set<Association>().Include("Customer").SingleOrDefault(x => x.OrganizationId== asso.organizationId && x.ExternalId == asso.ExternalId && x.AssociationType == asso.AssociationType);

现在我得出结论,虽然我使用不同的方法来检索异常是一样的。问题是映射我猜。感谢您的意见和建议。

Now I came to conclusion that though I use different methods for retrieving the exception is same. The problem is with mapping I guess. I appreciate your comments and suggestions.

推荐答案

尝试删除

Customer = new Customer();

协会构造函数。实例化导航参考是已知问题的一个来源(与实例化空导航集合是不错的)相反。这就是为什么你得到一个客户与默认值的原因。我不知道是否也解释了这个异常,但是我可以想象,当 Association 被加载并附加到上下文以及未初始化的由默认构造函数创建的客户 EF使用无效键检测相关实体:关联其中(我假设)键值!= 0 和相关的客户与密钥 == 0 (因为它从来没有被初始化为另一个值)。但是,在共享主键关联中,两个键值必须匹配。因为它们没有,它可能会导致异常(但是一个异常并不能很好地解决问题的根源)。

from the Association constructor. Instantiating navigation references is a source for known problems (in contrast to instantiating empty navigation collections which is fine). It is the reason why you get a Customer with default values. I'm not sure if it also explains the exception, but I could imagine that when the Association gets loaded and attached to the context together with the uninitialized Customer created by the default constructor EF detects related entities with invalid keys: The Association which has (I assume) a key value !=0 and the related Customer with a key ==0 (because it never has been initialized to another value). However, in a shared primary key association the two key values must match. Because they don't, it might cause the exception (however an exception that doesn't really point very well to the root of the problem).

只是一个猜测。

这篇关于问题加载父实体的子实体。单向映射和1到0..1与共享主键的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆