MVC5 Identity + multiple context in EntityFramework

前端 未结 1 1069
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 09:54

I have a MVC 5 application, which uses the Default Identity authentication. The user profile is part of our model, which means that there are several class which have a for

相关标签:
1条回答
  • 2020-12-18 10:36

    The solution I came up with recently is to use single context for both ASP.NET identity data and your business entities:

    public class DatabaseContext : IdentityDbContext<UserInfo>
    {
        public virtual DbSet<Comment> Comments { get; set; } // Your business entities
    
        public DatabaseContext()
            : base("name=DatabaseContext")
        {
        }
    }
    

    Notice that the DatabaseContext inherits from the IdentityDbContext<UserInfo>.

    There are some trade-offs with this approach: for example, your data access layer should reference Microsoft.AspNet.Identity.Core and Microsoft.AspNet.Identity.EntityFramework; however, having a single database context in your project makes things much easier if you are using dependency injection or Entity Framework migrations.

    0 讨论(0)
提交回复
热议问题