2015-12-28 16 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." 

は私の商人のモデルである:ここで

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

は、私はエラーを取得していますところである商人のために作成するコントローラであり、

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ですEntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

私は2つのDBを持つことを避けたいので、オプション2を試しています。あなたは、私はオプション3をやるべきだと思う場合は理由をアドバイスしてください、または私はこのエラーを取得していますなぜあなたが見れば、なぜ私に教えてくださいので、しかし、私はDBの管理に新しいです。前もって感謝します!

+1

あなたが言及したテーブルにPKを定義しましたか?例えば、フォロワーの下 –

+0

はい: [キー] 公共int型FollowerID {取得します。セット; } –

+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

[OK]を、私は私の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

関連する問題