合并IdentityDbContext和DbContext ASP.NET MVC [英] Merge IdentityDbContext and DbContext ASP.NET MVC

查看:61
本文介绍了合并IdentityDbContext和DbContext ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ASP.NET MVC项目中只有一个DbContext。我应该如何将默认的IdentityDbContext与我自己的代码优先DbContext合并?他们使用的是同一数据库。

I would like to only have one DbContext in my ASP.NET MVC project. How should I merge the default IdentityDbContext with my own code first DbContext? They are using the same database.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("SignalRChat")
        {
        }
    }

这是IdentityModel.cs类中提供的默认值。

This is the default one provided in the IdentityModel.cs class.

请执行下列操作?并附加我自己的DbSet?

Is it safe to do the following? And append my own DbSets?

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("SignalRChat")
    {
    }

    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
}

任何建议都非常感谢!

Any advice is much appreciated!

推荐答案

是的,最好在DbContext类中添加DbSet声明。通常,您可能会为相关的一组表创建一个上下文,当您从存储库中访问它时,例如

Yes, it is perfectly fine to add DbSet declarations in the DbContext classes. Normally you would maybe create a context for a related group of tables that makes sense when you access it from your repositories, e.g. ApplicationDbContext for user registration, login etc. another for Students and related entities.

如果用户有很多上下文,也许应该检查有界上下文模式 https://channel9.msdn.com/Events/TechEd/NorthAmerica / 2013 / DEV-B336#fbid = wH4n-081m_U

If you have many contexts maybe you should check for the bounded context "pattern" https://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-B336#fbid=wH4n-081m_U.

这篇关于合并IdentityDbContext和DbContext ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆