【问题标题】:EntityType 'FollowerMenuItemMerchant' has no key defined. Define the key for this EntityTypeEntityType 'FollowerMenuItemMerchant' 没有定义键。定义此 EntityType 的键
【发布时间】:2016-03-16 09:33:37
【问题描述】:

其他人问过这个问题,我检查了我是否犯了同样的错误,据我所知,我似乎没有。

这是我的 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);
}


}

你能看出哪里可能出错了吗?

【问题讨论】:

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


    【解决方案1】:

    哎呀,我找到了!我使用了小写的“key”,而不是大写的“Key”!呵呵!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 2013-08-04
      • 2012-12-10
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多