I have two classes:
public class Product
{
public Guid Id {get;set;}
public IList<Reference> References {get;set;}
}
public class Reference
{
public Guid Id {get;set;}
}
Both table has many to many relationship and I added in context file.
modelBuilder.Entity<Product>()
.HasMany<Reference>(p => p.References)
.WithMany()
.Map(pxar =>
{
pxar.MapLeftKey("Product_Id");
pxar.MapRightKey("Reference_Id");
pxar.ToTable("ProductsXReferences");
});
Not sure how to fix the problem. I checked my database tables and connectionstring in Web.Config, everything looks right.
You are missing the collection of Products on Reference. Try:
public class Product
{
public Guid Id {get;set;}
public ICollection<Reference> References {get;set;}
}
public class Reference
{
public Guid Id {get;set;}
public ICollection<Product> Products {get;set;}
}
modelBuilder.Entity<Product>()
.HasMany(p => p.References)
.WithMany(r => r.Products)
.Map(pxar =>
{
pxar.MapLeftKey("Product_Id");
pxar.MapRightKey("Reference_Id");
pxar.ToTable("ProductsXReferences");
});