Asp.net-Mvc
MVC/程式碼優先:如何將更多表添加到同一個數據庫上下文?
我使用的是 VS 2013 附帶的標準 MVC 模板。它有一個簡潔的會員提供程序,使使用外部登錄(Google、Facebook 等)變得輕而易舉。還有關於如何擴展
IdentityUser模型以添加新屬性(例如出生日期)的教程。我想將更多表(我的應用程序)添加到已經編碼的數據庫上下文中,以便享受相同的程式碼優先遷移功能。我該怎麼做?目前數據庫上下文定義如下:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } }
您正在使用 asp.net MVC5 identity 2 然後 ApplicationDbContext 已經存在於 IdentityModels.cs 中。因此您可以像這樣添加表(DbSet)。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("ApplicationDbContext", throwIfV1Schema: false) { } public DbSet<Department> Departments { get; set; } public DbSet<Student> Students { get; set; } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } }