From your code, is there any specific reason why a product Or Category should inherit from DbContext ? Also, try the below and it will work. The reason i am suspecting why it failed is "A part of the Composite Primary Key (in Product) is also a part of Foreign Key (Composite Fk) from Category"
public class SampleContext : DbContext
{
public IDbSet<Category> Categories { get; set; }
public IDbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
public class Category
{
[Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CategoryId { get; set; }
[Key, Column(Order = 1)]
public int ShopId { get; set; }
public string Name { get; set; }
}
public class Product
{
[Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductId { get; set; }
public int ShopId { get; set; }
public int CategoryId { get; set; }
[ForeignKey("CategoryId, ShopId")]
public virtual Category Category { get; set; }
}
Let me know if this what you are expecting. and First thing at place is EF Migration Runner should throw exception ????