2016-03-29 9 views
2

Ich erhalte diesen Fehler, wenn ich versuche, mich in meine ASP.NET MVC5 mit Identity Web Application anzumelden. Diese Anwendung war früher MVC4, bis ich wegen Identity auf MVC5 aktualisiert habe. In MVC4 verwendet es eine User.cs-Klasse, während in MVC5 mein ApplicationUser von IdentityUser abgeleitet ist.MVC 5 Mehrere Objektgruppen pro Typ werden nicht unterstützt

Mehrere Objektgruppen pro Typ werden nicht unterstützt. Die Objektsätze 'ApplicationUsers' und 'Users' können beide Instanzen des Typs 'RecreationalServicesTicketingSystem.Models.ApplicationUser' enthalten.

Ich lese aus diesem question, dass ich diese Linie zu entfernen brauchte, um mein Problem

public System.Data.Entity.DbSet<Manager.Models.ApplicationUser> IdentityUsers { get; set; }

zu beheben, aber nach dem Entfernen ich auf ‚Application‘ mehrere Fehler haben in meinem ApplicationUserController.cs sagen

'ApplicationDbContext' enthält keine Definition für 'ApplicationUsers' und keine Erweiterungsmethode 'ApplicationUsers' ein erstes Argument des Typs 'ApplicationDbContext' akzeptieren könnte

enter image description here

IdentityModels.cs

namespace RecreationalServicesTicketingSystem.Models 
{ 
    public class ApplicationUser 
    : IdentityUser<int, ApplicationUserLogin, 
     ApplicationUserRole, ApplicationUserClaim>, IUser<int> 
    { 
     public async Task<ClaimsIdentity> 
      GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager) 
     { 
      var userIdentity = await manager 
       .CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
      return userIdentity; 
     } 


     public bool IsAdministrator { get; set; } 
     [StringLength(50, MinimumLength = 1)] 

     public string LastName { get; set; } 
     [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")] 

     [Column("FirstName")] 
     public string FirstMidName { get; set; } 

     public string FullName 
     { 
      get { return FirstMidName + " " + LastName; } 
     } 
     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     public DateTime EnrollmentDate { get; set; } 
     public int DepartmentID { get; set; } 
     [ForeignKey("DepartmentID")] 
     public virtual Department Department { get; set; } 
     public int DepotID { get; set; } 
     [ForeignKey("DepotID")] 
     public virtual Depot Depot { get; set; } 
     public virtual ICollection<Ticket> Tickets { get; set; } 
    } 

    public class ApplicationUserLogin : IdentityUserLogin<int> { } 
    public class ApplicationUserClaim : IdentityUserClaim<int> { } 
    public class ApplicationUserRole : IdentityUserRole<int> { } 

    public class ApplicationRole : IdentityRole<int, ApplicationUserRole>, IRole<int> 
    { 
     public string Description { get; set; } 

     public ApplicationRole() { } 
     public ApplicationRole(string name) 
      : this() 
     { 
      this.Name = name; 
     } 

     public ApplicationRole(string name, string description) 
      : this(name) 
     { 
      this.Description = description; 
     } 
    } 


    public class ApplicationDbContext 
     : IdentityDbContext<ApplicationUser, ApplicationRole, int, 
     ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
    { 
     public ApplicationDbContext() 
      : base("DefaultConnection") 
     { 
     } 

     static ApplicationDbContext() 
     { 
      Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); 
     } 

     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 

     public class ApplicationUserStore : 
    UserStore<ApplicationUser, ApplicationRole, int, 
    ApplicationUserLogin, ApplicationUserRole, 
    ApplicationUserClaim>, IUserStore<ApplicationUser, int>, 
    IDisposable 
     { 
      public ApplicationUserStore() : this(new IdentityDbContext()) 
      { 
       base.DisposeContext = true; 
      } 

      public ApplicationUserStore(DbContext context) 
       : base(context) 
      { 
      } 
     } 


     public class ApplicationRoleStore 
      : RoleStore<ApplicationRole, int, ApplicationUserRole>, 
      IQueryableRoleStore<ApplicationRole, int>, 
      IRoleStore<ApplicationRole, int>, IDisposable 
     { 
      public ApplicationRoleStore() 
       : base(new IdentityDbContext()) 
      { 
       base.DisposeContext = true; 
      } 

      public ApplicationRoleStore(DbContext context) 
       : base(context) 
      { 
      } 
     } 




     public DbSet<Ticket> Tickets { get; set; } 
     public DbSet<Category> Categories { get; set; } 
     public DbSet<Department> Departments { get; set; } 
     public DbSet<Depot> Depots { get; set; } 

     public System.Data.Entity.DbSet<RecreationalServicesTicketingSystem.Models.ApplicationUser> ApplicationUsers { get; set; } 
    } 
} 

ApplicationUserController.cs

public class ApplicationUserController : Controller 
    { 
     private ApplicationDbContext db = new ApplicationDbContext(); 

     // GET: ApplicationUser 
     public async Task<ActionResult> Index() 
     { 
      var applicationUsers = db.ApplicationUsers.Include(a => a.Department).Include(a => a.Depot); 
      return View(await applicationUsers.ToListAsync()); 
     } 

     // GET: ApplicationUser/Details/5 
     public async Task<ActionResult> Details(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      ApplicationUser applicationUser = await db.ApplicationUsers.FindAsync(id); 
      if (applicationUser == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(applicationUser); 
     } 

     // GET: ApplicationUser/Create 
     public ActionResult Create() 
     { 
      ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName"); 
      ViewBag.DepotID = new SelectList(db.Depots, "DepotID", "DepotName"); 
      return View(); 
     } 

     // POST: ApplicationUser/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 async Task<ActionResult> Create([Bind(Include = "Id,IsAdministrator,LastName,FirstMidName,EnrollmentDate,DepartmentID,DepotID,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] ApplicationUser applicationUser) 
     { 
      if (ModelState.IsValid) 
      { 
       db.ApplicationUsers.Add(applicationUser); 
       await db.SaveChangesAsync(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", applicationUser.DepartmentID); 
      ViewBag.DepotID = new SelectList(db.Depots, "DepotID", "DepotName", applicationUser.DepotID); 
      return View(applicationUser); 
     } 

     // GET: ApplicationUser/Edit/5 
     public async Task<ActionResult> Edit(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      ApplicationUser applicationUser = await db.ApplicationUsers.FindAsync(id); 
      if (applicationUser == null) 
      { 
       return HttpNotFound(); 
      } 
      ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", applicationUser.DepartmentID); 
      ViewBag.DepotID = new SelectList(db.Depots, "DepotID", "DepotName", applicationUser.DepotID); 
      return View(applicationUser); 
     } 

     // POST: ApplicationUser/Edit/5 
     // 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 async Task<ActionResult> Edit([Bind(Include = "Id,IsAdministrator,LastName,FirstMidName,EnrollmentDate,DepartmentID,DepotID,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] ApplicationUser applicationUser) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(applicationUser).State = EntityState.Modified; 
       await db.SaveChangesAsync(); 
       return RedirectToAction("Index"); 
      } 
      ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", applicationUser.DepartmentID); 
      ViewBag.DepotID = new SelectList(db.Depots, "DepotID", "DepotName", applicationUser.DepotID); 
      return View(applicationUser); 
     } 

     // GET: ApplicationUser/Delete/5 
     public async Task<ActionResult> Delete(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      ApplicationUser applicationUser = await db.ApplicationUsers.FindAsync(id); 
      if (applicationUser == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(applicationUser); 
     } 

     // POST: ApplicationUser/Delete/5 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> DeleteConfirmed(int id) 
     { 
      ApplicationUser applicationUser = await db.ApplicationUsers.FindAsync(id); 
      db.ApplicationUsers.Remove(applicationUser); 
      await db.SaveChangesAsync(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 
    } 
} 
+1

Nachdem Sie diese Zeile entfernt haben, können Sie in Ihrem Kontext nach einer 'Users'-Eigenschaft suchen? Diese Eigenschaft wäre auch ein 'DbSet' von' ApplicationUser'. Wenn Sie es finden, können Sie es einfach in Ihrem Controller verwenden. –

Antwort

2

Ihre Klasse Darstellung

gefunden
public class ApplicationDbContext 
    : IdentityDbContext<ApplicationUser, ApplicationRole, int, 
    ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
{...} 

bereits eine Users Eigenschaft

public virtual IDbSet<TUser> Users { get; set; } 

wo TUser für Sie ApplicationUser sein wird.

Sie müssen alle Instanzen ersetzen, wo man db.ApplicationsUsers-db.Users

Beispiel:

var applicationUsers = db.ApplicationUsers.Include(a => a.Department).Include(a => a.Depot); 

Änderungen:

var applicationUsers = db.Users.Include(a => a.Department).Include(a => a.Depot); 
+0

Wenn ich alle Instanzen von db.ApplicationUsers zu db.Users ersetze, bekomme ich einen Fehler in dieser Zeile 'ApplicationUser applicationUser = erwarten db.Users.FindAsync (id);' dann bekomme ich 'IDbSet 'enthält keine Definition für 'FindAsync' und keine Erweiterungsmethode 'FindAsync', die ein erstes Argument des Typs 'IDbSet ' akzeptiert, gefunden werden kann. – TykiMikk

+0

was korrekt ist. IDbSet hat diese asynchrone Methode nicht – Nkosi

+0

Also muss ich meine Controller neu erstellen, um keine asynchronen Controller zu haben? – TykiMikk

1

Die IdentityDbContext Klasse bereits eine DbSet<ApplicationUser> Eigenschaft hat Users genannt .

Also, wenn Ihre eigene Context-Klasse von IdentityDbContext erbt, gibt es keine Notwendigkeit, Ihre eigene DbSet<ApplicationUser> Eigenschaft hinzuzufügen (und in der Tat, wenn Sie dies tun, werden Sie den Fehler "mehrere Objektgruppen").

Die Users-Eigenschaft wird in Ihrem eigenen Kontext verfügbar sein, Sie können diese stattdessen verwenden.

+0

Verstehen Sie nicht ganz, was Sie unter 'Die Eigenschaft 'Benutzer' verstehen, die in Ihrem eigenen Kontext verfügbar ist. Verwenden Sie stattdessen diese Option. So entferne ich' public System.Data.Entity.DbSet ApplicationUsers {erhalten; einstellen; } 'und benutze welchen? – TykiMikk

+0

So funktioniert die Vererbung. Jede Klasse, die von "IdentityDbContext" erbt, hat Zugriff auf alle nicht privaten Eigenschaften. Die 'IdentityDbContext' Klasse hat eine nicht-private 'Users'-Eigenschaft, die ein' DbSet 'ist. Wenn Sie also Ihren eigenen Kontext verwenden (der von "IdentityDbContext" ableitet), steht Ihnen die Eigenschaft "Users" zur Verfügung. –

+0

Aber nach dem Ändern von 1db.ApplicationsUsers1 auf 1db.Users 'bekomme ich diese 'IDbSet ' enthält keine Definition für 'FindAsync' und keine Erweiterungsmethode 'FindAsync' könnte ein erstes Argument vom Typ 'IDbSet akzeptieren gefunden werden " – TykiMikk

Verwandte Themen