Entity framework 发布加载父实体的子实体。单向映射和1到0..1与共享主键的关系?

Entity framework 发布加载父实体的子实体。单向映射和1到0..1与共享主键的关系?,entity-framework,ef-code-first,entity-framework-6,entity-framework-ctp5,Entity Framework,Ef Code First,Entity Framework 6,Entity Framework Ctp5,当我尝试加载父实体的子实体时,它会加载默认值。如果我尝试显式加载,它会引发异常 违反了多重性约束。关系“CodeFirstNamespace.Association\u Customer”的角色“Association\u Customer\u Target”的多重性为1或0..1。检索复杂图形的子实体时引发此异常 我有一个图形关联,它有一个子实体客户,关系为一对零或一,并且有一个独立关联。主键*是共享的。我用的是EF6。已启用延迟加载 public class Association {

当我尝试加载父实体的子实体时,它会加载默认值。如果我尝试显式加载,它会引发异常 违反了多重性约束。关系“CodeFirstNamespace.Association\u Customer”的角色“Association\u Customer\u Target”的多重性为1或0..1。检索复杂图形的子实体时引发此异常

我有一个图形关联,它有一个子实体客户,关系为一对零或一,并且有一个独立关联主键*是共享的。我用的是EF6。已启用延迟加载

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>();
  }
}
公共类客户
{
公共虚拟长Id{get;set;}//共享主键
公共虚拟ICollection项{get;set;}
公共虚拟ICollection投诉{get;set;}
公众客户()
{
项目=新列表();
投诉=新列表();
}
}
映射是单向的:

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();
  } 
}
公共类AssociationMapping:EntityTypeConfiguration
{
public AssociationMapping():base()
{
HasKey(x=>x.Id);
属性(x=>x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
属性(x=>x.ExternalId).IsRequired();
属性(x=>x.OrganizationId).IsRequired();
属性(x=>x.AssociationType);
has可选(x=>x.Customer).WithRequired().WillCascadeOnDelete();
}
}
公共类CustomerMapping:EntityTypeConfiguration
{
公共CustomerMapping():base()
{
HasKey(x=>x.Id);
属性(x=>x.Id);
HasMany(x=>x.Items)
.WithOptional()
.HasForeignKey(key=>key.CustomerId)
.WillCascadeOnDelete();
有很多(x=>x.1)
.WithOptional()
.HasForeignKey(key=>key.CustomerId)
.WillCascadeOnDelete();
} 
}
当我加载关联实体时,它会完全加载,但当我尝试显式加载Customer时,子实体Customer会加载默认值,它会引发异常

 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);
var dbassociation=Single(x=>x.OrganizationId==asso.OrganizationId&&x.ExternalId==asso.ExternalId&&x.AssociationType==asso.AssociationType);
dbassociation.Customer=Single(x=>x.id==dbassociation.id);
[更新:单一方法]

public TEntity Single<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>>criteria) {
  return Context.Set<TEntity>().SingleOrDefault(criteria);   }
public tenty Single(System.Linq.Expressions.Expressioncriteria){
返回Context.Set().SingleOrDefault(条件);}
出于测试的目的,我尝试通过删除关联类中客户属性上的虚拟对象来增加负载,并尝试了以下操作,但它引发了相同的异常

Context.Configuration.LazyLoadingEnabled = false;
Context.Entry<Association>(dbassociation).Reference<Customer>(pa => pa.Customer).Load();
Context.Configuration.LazyLoadingEnabled=false;
Context.Entry(dbassociation).Reference(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);
var dbassociation=Context.Set().Include(“客户”).SingleOrDefault(x=>x.OrganizationId==asso.OrganizationId&&x.ExternalId==asso.ExternalId&&x.AssociationType==asso.AssociationType);
现在我得出结论,尽管我使用不同的方法来检索异常,但它们是相同的。我想问题在于映射。感谢您的评论和建议。

请尝试删除

Customer = new Customer();
关联
构造函数。实例化导航引用是已知问题的根源(与实例化空的导航集合相反,空的导航集合很好)。这就是为什么您会得到一个带有默认值的
客户
。我不确定它是否也解释了异常,但我可以想象,当
关联
被加载并与默认构造函数创建的未初始化的
客户
一起附加到上下文时,EF会检测到具有无效键的相关实体:
关联
,它具有(我假设)一个键值
=0
和键为
==0的相关
客户
(因为它从未初始化为其他值)。但是,在共享主键关联中,两个键值必须匹配。因为他们没有这样做,所以可能会导致异常(然而,异常并不能很好地指出问题的根源)


猜一猜。

关联和客户之间的关系到底是什么?是1:1、一对多等吗?哪个表有FK引用?我已经提到它是1:0..1主键在关联和客户之间共享。客户有fk引用。从导航属性中删除
virtual
关键字并不意味着立即加载。我的意思是在删除virtual后,我按照下面的步骤进行了立即加载。没有。单一的
方法看起来怎么样?