ASP.NET Identity DbContext-Verwechslung
Eine Standard-MVC 5-App enthält diesen Code in IdentityModels.cs. Dieser Code gilt für alle ASP.NET Identity-Vorgänge für die Standardvorlagen:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
Wenn ich einen neuen Controller mithilfe von Ansichten mit Entity Framework einbaue und im Dialogfeld einen "Neuen Datenkontext ..." erstelle, wird Folgendes für mich generiert:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class AllTheOtherStuffDbContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
{
}
public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
}
}
Wenn ich einen anderen Controller + View mit EF einbaue, beispielsweise für ein Tiermodell, wird diese neue Zeile direkt darunter automatisch generiertpublic System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
- so was:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class AllTheOtherStuffDbContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
{
}
public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
public System.Data.Entity.DbSet<WebApplication1.Models.Animal> Animals { get; set; }
}
}
ApplicationDbContext
(für alle ASP.NET Identity-Inhalte) erbt vonIdentityDbContext
was wiederum erbt vonDbContext
. AllOtherStuffDbContext
(für meine eigenen Sachen) erbt vonDbContext
.
Meine Frage lautet also:
Welche dieser beiden (ApplicationDbContext
undAllOtherStuffDbContext
) soll ich für alle meine anderen eigenen Modelle verwenden? Oder soll ich einfach die automatisch generierte Standardeinstellung verwendenApplicationDbContext
da es kein Problem sein sollte, es zu verwenden, da es von der Basisklasse stammtDbContext
, oder wird es etwas Overhead geben? Sie sollten nur eine verwendenDbContext
Objekt in Ihrer App für alle Ihre Modelle (ich habe dies irgendwo gelesen), so sollte ich nicht einmal in Betracht ziehen, beide zu verwendenApplicationDbContext
undAllOtherStuffDbContext
in einer einzigen App? Oder was ist die beste Vorgehensweise in MVC 5 mit ASP.NET Identity?