2015-12-28 17 views
6

Tôi gặp phải các lỗi này khi cố tạo người bán.EntityType 'IdentityUserLogin' không có khóa được xác định. Xác định khóa cho EntityType này

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." 

Đây là mô hình thương của tôi:

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

Dưới đây là bộ điều khiển tạo cho thương gia, đó là nơi tôi đang nhận được lỗi:

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

Dưới đây là DBContext tôi:

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>(); 


     } 
    } 
} 

Tôi tr ying để làm theo ví dụ (option2) trong liên kết này: EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

Tôi đang thử Tùy chọn 2 vì tôi muốn tránh có hai DB. Nhưng tôi mới để quản lý một DB vì vậy nếu bạn nghĩ rằng tôi nên làm Option 3 xin vui lòng tư vấn là tại sao, hoặc nếu bạn thấy lý do tại sao tôi nhận được lỗi này xin vui lòng cho tôi biết lý do tại sao. Cảm ơn trước!

+1

Bạn đã xác định PK cho các bảng mà bạn đã đề cập chưa? –

+0

Có ví dụ, dưới người theo dõi: [Key] public int FollowerID {get; bộ; } –

+0

Tùy chọn 2 là tùy chọn dễ dàng nhất. Đi cho nó. –

Trả lời

0

Tôi nghĩ rằng bạn sẽ có được sai lầm vì các thuộc tính khóa ngoại của bạn đang không ở trong vị trí chính xác (và có sai tên), thay vì điều này:

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

Bạn cần phải làm điều này:

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

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

ID là chìa khóa nước ngoài cho tổ chức ảo, không phải là cách khác xung quanh.

+0

Wow đó là lỗi tôi đã bỏ qua, cảm ơn! –

+0

Đừng quên đánh dấu câu trả lời là đã chấp nhận nếu sự cố của bạn được giải quyết :) –

+0

Thực ra, cả hai cách sẽ hoạt động, nhưng bạn phải tham chiếu đúng thuộc tính. Trong mã của OP, nó sẽ cần phải là '[ForeignKey (" ApplicationUserId ")]', ngụ ý rằng thuộc tính navigation có 'ApplicationUserId' là khóa ngoài của nó. –

15

Ok Tôi cố định vấn đề của tôi bằng cách thêm này vào lớp DBContext tôi.

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

Nó đã làm việc cho tôi. Cảm ơn! – veyselsahin

Các vấn đề liên quan