2015-12-28 96 views
6

嘗試創建商家時出現這些錯誤。EntityType'IdentityUserLogin'沒有定義密鑰。定義此EntityType的密鑰

FlavorPing.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. 

FlavorPing.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. 

UserLogins: EntityType: EntitySet 'UserLogins' is based on type 'IdentityUserLogin' that has no keys defined. 

UserRoles: EntityType: EntitySet 'UserRoles' is based on type 'IdentityUserRole' that has no keys defined." 

這裏是我的淨價模式:

namespace FlavorPing.Models 
{ 
    public class Merchant 
    { 
     //Meant to inherit identity. 
     //[ForeignKey("ApplicationUserId")] 
     public string ApplicationUserId { get; set; } 
     [ForeignKey("ApplicationUser")] 
     public virtual List<ApplicationUser> ApplicationUser { get; set; } 


     [Key] 
     public int MerchantID { get; set; } 

     [Required] 
     [Display(Name = "Business Name")] 
     public string MerchantName { get; set; } 

     [Required] 
     [Display(Name = "Email")] 
     [DataType(DataType.EmailAddress)] 
     public string email { get; set; } 

     //need to create formatting here. 
     [Required] 
     [Display(Name = "Web Site Link")] 
     public string website { get; set; } 

     //public int MenuItemID { get; set; } 


     public virtual List<MenuItem> MenuItems { get; set; } 

     public virtual MerchantDetails MerchantDetails { get; set; } 

     public ICollection<FollowerMenuItemMerchant> FollowerMenuItemMerchants { get; set; } 
    } 
} 

這裏是商戶創造控制器,這就是我得到的錯誤:

// POST: Merchants/Create 
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "MerchantID,MerchantName,email,website")] Merchant merchant) 
{ 
    if (ModelState.IsValid) 
    { 
     merchant.ApplicationUserId = User.Identity.GetUserId(); 
     db.Merchants.Add(merchant); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return View(merchant); 
} 

這裏是我的DbContext:

namespace FlavorPing.Models 
{ 
    public class FlavorPingContext : IdentityDbContext 
    { 

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

     public DbSet<IdentityUserLogin> UserLogins { get; set; } 
     public DbSet<IdentityUserClaim> UserClaims { get; set; } 
     public DbSet<IdentityUserRole> UserRoles { 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); 


      builder.Conventions.Remove<PluralizingTableNameConvention>(); 
      builder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 


     } 
    } 
} 

我是tr ying按照這個鏈接中的例子(option2):EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

我想要選項2,因爲我想避免有兩個數據庫。但我是管理數據庫的新手,所以如果你認爲我應該做選項3,請告訴我爲什麼,或者如果你明白爲什麼我得到這個錯誤,請告訴我爲什麼。提前致謝!

+1

您是否爲您提到的表格定義了PK? –

+0

是例如,在Follower下: [Key] public int FollowerID {get;組; } –

+0

選項2是最簡單的選項。去吧。 –

回答

0

我覺得你得到的錯誤,因爲你的外鍵的屬性是不是在正確的位置(和有錯誤的名稱),而不是這樣的:

public string ApplicationUserId { get; set; } 
[ForeignKey("ApplicationUser")] 
public virtual List<ApplicationUser> ApplicationUser { get; set; } 

你需要這樣做:

[ForeignKey("ApplicationUser")] 
public string ApplicationUserId { get; set; } 

public virtual List<ApplicationUser> ApplicationUser { get; set; } 

ID是虛擬實體的外鍵,而不是其他方式。

+0

哇,這是我完全忽略的錯誤,謝謝! –

+0

如果問題得到解決,請不要忘記將答案標記爲已接受:) –

+0

其實,無論哪種方式都可以,但必須引用正確的屬性。在OP的代碼中,它需要是'[ForeignKey(「ApplicationUserId」)]',這意味着導航屬性具有'ApplicationUserId',因爲它是外鍵。 –

15

好吧我解決了我的問題,將此添加到我的DBContext類。

builder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId); 
    builder.Entity<IdentityRole>().HasKey<string>(r => r.Id); 
    builder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }); 
+0

它爲我工作。謝謝! – veyselsahin

相關問題