既定の MVC 5 アプリには、IdentityModels.cs に次のコードが付属しています。このコードは、既定のテンプレートのすべての ASP.NET Identity 操作用です。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
Entity Framework でビューを使用して新しいコントローラーをスキャフォールディングし、ダイアログで「新しいデータ コンテキスト...」を作成すると、次のように生成されます。
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; }
}
}
EF を使用して別のコントローラーとビューをスキャフォールディングすると、たとえば Animal モデルの場合、この新しい行は次のpublic System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }ようにすぐ下に自動生成されます。
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(すべての ASP.NET Identity のものについて)IdentityDbContextは、次に継承するものから継承しDbContextます。
AllOtherStuffDbContext(私自身のもののために)から継承しDbContextます。
だから私の質問は:
これら 2 つ (ApplicationDbContextとAllOtherStuffDbContext) のうち、他のすべての独自のモデルに使用する必要があるのはどれですか? ApplicationDbContextまたは、基本クラスから派生しているため使用しても問題ないため、自動生成されたデフォルトを使用する必要DbContextがありますか、それともオーバーヘッドが発生しますか? すべてのモデルに対してアプリで1 つのオブジェクトのみを使用する必要がありますDbContext(これはどこかで読んだことがあります) 。または、ASP.NET Identity を使用した MVC 5 のベスト プラクティスは何ですか?ApplicationDbContextAllOtherStuffDbContext