ca9*_*3d9 30 c# asp.net asp.net-mvc entity-framework asp.net-mvc-5
我正在创建一个Asp.Net MVC 5网站.我需要在ApplicationUser中添加自定义字段并将其与其他模型关联(添加外键).我想我应该只使用一种上下文类型.但是,代码脚手架已生成以下ApplicationDbContext
类.我可以把我所有public DbSet<...> ... { get; set; }
的课都放进去吗?还是有更好的模式?
namespace MyApp.Models
{
// You can add profile data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : User
{
}
public class ApplicationDbContext : IdentityDbContextWithCustomUser<ApplicationUser>
{
}
}
Run Code Online (Sandbox Code Playgroud)
sti*_*ink 10
我建议将它们分开.没有理由将系统的两个部分连接在一起.要添加另一个DbContext,只需将文件添加到名为YourContext.cs的模型中.
public class YourContext: DbContext
{
public YourContext() : base("name=YourContext")
{
}
// Add a DbSet for each one of your Entities
public DbSet<Room> Rooms { get; set; }
public DbSet<Meal> Meals { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后在根web.config中
<add name="YourContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=YourContext; Integrated Security=True"" providerName="System.Data.SqlClient" />
在程序包管理器控制台中运行enable-migrations时,将询问您要迁移哪个dbcontext.选择你的上下文.
编辑:无需添加实体框架为您执行此操作的repos /工作单元.