EntityType'IftererMenuItemMerchant'没有定义键。定义此EntityType

时间:2015-12-10 16:37:20

标签: entity-framework asp.net-mvc-5

其他人已经问过这个问题,我检查了一下我是否也犯了同样的错误,而且从我所知道的情况来看,我似乎并非如此。

这是我的 FollowerMenuItemMerchant 类。

      public class FollowerMenuItemMerchant
{
    [key, Column(Order = 0)]
    public int FollowerID { get; set; }


    [key, Column(Order = 1)]
    public int MenuItemID { get; set; }


    [key, Column(Order = 2)]
    public int MerchantID { get; set; }

    public virtual Follower Follower { get; set; }
    public virtual MenuItem MenuItem { get; set; }
    public virtual Merchant Merchant { get; set; }

}

这是 Context 类:

     public class FlavorPingContext : IdentityDbContext<ApplicationUser>
{

    public FlavorPingContext() : base("name=FlavorPingContext")
    {
    }

    public System.Data.Entity.DbSet<FlavorPing.Models.Merchant> Merchants { get; set; }

    public System.Data.Entity.DbSet<FlavorPing.Models.MenuItem> MenuItems { get; set; }

    public System.Data.Entity.DbSet<FlavorPing.Models.MerchantDetails> MerchantDetails { get; set; }

    public System.Data.Entity.DbSet<FlavorPing.Models.Follower> Followers { get; set; }

    public System.Data.Entity.DbSet<FlavorPing.Models.FollowerMenuItemMerchant> FollowerMenuItemMerchants { get; set; }


     protected override void OnModelCreating(DbModelBuilder builder) {
    // Primary keys
    builder.Entity<Follower>().HasKey(q => q.FollowerID);
    builder.Entity<MenuItem>().HasKey(q => q.MenuItemID);
    builder.Entity<Merchant>().HasKey(q => q.MerchantID);
    builder.Entity<FollowerMenuItemMerchant>().HasKey(q => 
        new { 
            q.FollowerID, q.MenuItemID, q.MerchantID
        });

    // Relationships
    builder.Entity<FollowerMenuItemMerchant>()
        .HasRequired(t => t.Follower)
        .WithMany(t => t.FollowerMenuItemMerchants)
        .HasForeignKey(t => t.FollowerID);

    builder.Entity<FollowerMenuItemMerchant>()
        .HasRequired(t => t.MenuItem)
        .WithMany(t => t.FollowerMenuItemMerchants)
        .HasForeignKey(t => t.MenuItemID);

    builder.Entity<FollowerMenuItemMerchant>()
    .HasRequired(t => t.Merchant)
    .WithMany(t => t.FollowerMenuItemMerchants)
    .HasForeignKey(t => t.MerchantID);
}


}

你能看到可能出现错误的地方吗?

1 个答案:

答案 0 :(得分:0)

哎呀,我找到了!我使用小写&#34;键&#34;,而不是大写&#34;键&#34;!卫生署!