2015-12-28 7 views
6

के लिए कुंजी को परिभाषित करें मुझे एक व्यापारी बनाने का प्रयास करते समय इन त्रुटियों को प्राप्त हो रहा है।EntityType 'IdentityUserLogin' की कोई कुंजी परिभाषित नहीं है। इस EntityType

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 है:

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 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 हूँ इस लिंक में उदाहरण (विकल्प 2) का पालन करने के लिए ying: EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

मैं विकल्प 2 का प्रयास कर रहा हूं क्योंकि मैं दो डीबी होने से बचना चाहता हूं। लेकिन मैं डीबी के प्रबंधन के लिए नया हूं इसलिए यदि आपको लगता है कि मुझे विकल्प 3 करना चाहिए तो कृपया सलाह दें कि क्यों, या यदि आप देखते हैं कि मुझे यह त्रुटि क्यों मिल रही है तो कृपया मुझे बताएं क्यों। अग्रिम में धन्यवाद!

+1

क्या आपने उल्लेख किया है कि आपने टेबल के लिए पीके को परिभाषित किया है? –

+0

हाँ उदाहरण के लिए, अनुयायी के तहत: [कुंजी] सार्वजनिक 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; } 

आईडी वर्चुअल इकाई की विदेशी कुंजी है, न कि दूसरी तरफ।

+0

वाह कि एक त्रुटि थी जिसे मैंने पूरी तरह से अनदेखा किया, धन्यवाद! –

+0

अगर आपकी समस्या हल हो जाती है तो उत्तर को स्वीकार करने के लिए मत भूलना :) –

+0

असल में, कोई भी तरीका काम करेगा, लेकिन आपको सही संपत्ति का संदर्भ देना होगा। ओपी के कोड में, इसे '[विदेशीकी (" ApplicationUserId ")]' होना चाहिए, जिसका अर्थ यह है कि नेविगेशन प्रॉपर्टी में 'ApplicationUserId' है क्योंकि यह विदेशी कुंजी है। –

15

ठीक है मैंने इसे अपनी डीबीसीएन्टेक्स्ट कक्षा में जोड़कर अपनी समस्या तय की है।

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

संबंधित मुद्दे

 संबंधित मुद्दे