Issue loading child entity of a parent entity. Unidirectional mapping and 1 to 0..1 relationship with a Shared primary Key?

ef-code-first entity-framework entity-framework-6 entity-framework-ctp5

Question

When I attempt to load a child entity of a parent object, default values are loaded. If I attempt to load explicitly, exception violated the multiplicity limit. is thrown. The relationship "CodeFirstNamespace.Association Customer" has a multiplicity of 1 or 0..1 for the role "Association Customer Target." When a complex graph's child entities are being retrieved, this exception is thrown.

I have a graph with the nodes Association, Customer, one to one or zero, and Unrelated organizations, which are the child entities. Zzz-33-zzz is *first crucial*. I employ EF6. Loading slowly 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();
   }
}

patron class.

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>();
  }
}

Maps have only one direction:

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();
  } 
}

The child entity Customer gets loaded with default values when I load my association entity, however when I try to specifically load Customer, an exception is raised.

 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: One Approach]

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

I tried to eager load for testing purposes by deleting the virtual on Customer property from the association class, but it still throws the same exception.

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

I also attempted, but the same problem still occurs.

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

Now I've come to the conclusion that the exception is the same even though I retrieve it using various techniques. I suppose that mapping is the issue. I value your opinions and recommendations.

1
2
11/26/2013 3:58:35 PM

Accepted Answer

ZZZ_tmp
11
11/26/2013 6:43:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow