加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – IdentityDbContext vs Reb的DbContext

发布时间:2020-12-16 07:38:37 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试构建我的IRepository接口和具体类EntityFrameworkGenericRepository,但我对DbContext有一个问题. 在EFGenericRepository中的以前版本的MVC中,您将引用EntityFramework并使用DbContext,这就是它. 现在我正在使用具有ASP.NET标识的MVC5,我需要为Iden
我正在尝试构建我的IRepository接口和具体类EntityFrameworkGenericRepository,但我对DbContext有一个问题.

在EFGenericRepository中的以前版本的MVC中,您将引用EntityFramework并使用DbContext,这就是它.

现在我正在使用具有ASP.NET标识的MVC5,我需要为IdentityDbContext切换DbContext,但这意味着我需要引用ASP.NET标识.

有没有办法解决这个问题,还是完全没问题?

构造函数和属性的片段

public class EntityFrameworkGenericRepository<T> : IGenericRepository<T> where T: class
{
    protected IdentityDbContext<ApplicationUser>Context = null;

    protected DbSet<T> DbSet
    {
        get
        {
            return Context.Set<T>();
        }
    }


    public EntityFrameworkGenericRepository(IdentityDbContext<ApplicationUser> context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        Context = context;
    }

这是我的WebAppDbContext:

public class WebAppDbContext : IdentityDbContext<ApplicationUser>
{
    public WebAppDbContext()
        : base(nameOrConnectionString: "DefaultConnection") { }

    public DbSet<User> User { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        base.OnModelCreating(modelBuilder);
    }
}

解决方法

这看起来很好.您有任何具体问题吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读