System.Data.SqlClient.SqlException: Invalid column name 'Reference_Id'

asp.net-mvc c# ef-code-first entity-framework-6

Question

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.

1
0
6/29/2016 4:05:26 PM

Popular Answer

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

https://msdn.microsoft.com/en-us/data/hh134698.aspx

0
6/29/2016 8:37:21 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