合并MyDbContext与IdentityDbContext [英] Merge MyDbContext with IdentityDbContext

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

问题描述

我有一个分开的数据Accass层类库项目MyDbContext。我有一个ASP.NET MVC 5项目,默认IdentityDbContext。这两种情况下使用相同的数据库,我想用AspNetUsers表为我的一些表的外键。
所以,我想合并这两个背景下,我想用ASP.NET身份了。

我怎样才能做到这一点?

请咨询,

这是我的上下文合并后:

 公共类CrmContext:IdentityDbContext< CrmContext.ApplicationUser> //的DbContext
{
    公共类ApplicationUser:IdentityUser
    {
        公众的Int16区{搞定;组; }
        公共BOOL假日{搞定;组; }
        公共BOOL CanBePublic {搞定;组; }
        公共字符串名字{获得;组; }
        公共字符串名字{获得;组; }
    }    公共CrmContext()
        :基地(DefaultConnection)
    {    }    公共DbSet<外壳>案例{搞定;组; }
    公共DbSet< CaseLog> CaseLog {搞定;组; }
    公共DbSet<&评论GT;评论{搞定;组; }
    公共DbSet<参数>参数{搞定;组; }
    公共DbSet< SIGN>注册{搞定;组; }
    公共DbSet<模板>模板{搞定;组; }
    公共DbSet<读取和GT;阅读{搞定;组; }    保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
    {
        modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
    }
}

下面是我的RepositoryBase类:

 公共类RepositoryBase< TContext,TEntity> :IRepositoryBaseAsync< TEntity>中了IDisposable
        其中,TContext:IdentityDbContext< CrmContext.ApplicationUser> //的DbContext
        其中,TEntity:类
    {
        私人只读TContext _context;
        私人只读IObjectSet< TEntity> _objectSet;        保护TContext上下文
        {
            {返回_context; }
        }        公共RepositoryBase(TContext上下文)
        {
            如果(上下文!= NULL)
            {
                _context =背景;
                //这是错误:
                _objectSet =(_context为IObjectContextAdapter).ObjectContext.CreateObjectSet< TEntity>();
             }
             其他
             {
                 抛出新的NullReferenceException(上下文不能为空);
             }
         }
     }

类型的异常'System.Data.Entity.ModelConfiguration.ModelValidationException 发生在EntityFramework.dll但在用户code没有处理

其他信息:模型的生成过程中检测到一个或多个验证错误:

更新:我找到了解决办法

我不得不删除这个命名约定:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
    modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
}

和迁移EF CF模型分贝,与asp.net身份nameing约定重命名我的表。
它现在的工作!


解决方案

  1. 将在 ApplicationUser 定义您的DAL。

  2. 继承你的 MyDbContext IdentityDbContext< ApplicationUser> IdentityDbContext

  3. OnModelCreating - 提供外键信息

  4. MyDbContext 在创建的UserManager< ApplicationUser>

I have a MyDbContext in a separated Data Accass Layer class library project. And I have an ASP.NET MVC 5 project with a default IdentityDbContext. The two context use the same database, and I want to use AspNetUsers table to foreign key for some my tables. So I would like to merge the two Context, and I want to use ASP.NET Identity too.

How can I do this?

Please advice,

This is my Context after merge:

public class CrmContext : IdentityDbContext<CrmContext.ApplicationUser> //DbContext
{
    public class ApplicationUser : IdentityUser
    {
        public Int16 Area { get; set; }
        public bool Holiday { get; set; }
        public bool CanBePublic { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public CrmContext()
        : base("DefaultConnection")
    {

    }

    public DbSet<Case> Case { get; set; }
    public DbSet<CaseLog> CaseLog { get; set; }
    public DbSet<Comment> Comment { get; set; }
    public DbSet<Parameter> Parameter { get; set; }
    public DbSet<Sign> Sign { get; set; }
    public DbSet<Template> Template { get; set; }
    public DbSet<Read> Read { get; set; }

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

Here is my RepositoryBase class:

    public class RepositoryBase<TContext, TEntity> : IRepositoryBaseAsync<TEntity>, IDisposable
        where TContext : IdentityDbContext<CrmContext.ApplicationUser> //DbContext
        where TEntity : class
    {
        private readonly TContext _context;
        private readonly IObjectSet<TEntity> _objectSet;

        protected TContext Context
        {
            get { return _context; }
        }

        public RepositoryBase(TContext context)
        {
            if (context != null)
            {
                _context = context;
                //Here it is the error:
                _objectSet = (_context as IObjectContextAdapter).ObjectContext.CreateObjectSet<TEntity>();
             }
             else
             {
                 throw new NullReferenceException("Context cannot be null");
             }
         }
     }

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:

Update: I found the solution.

I had to remove this naming convention:

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

And migrate the ef cf model to db, to rename my tables with the nameing conventions of asp.net identity. It is working now!

解决方案

  1. Move the ApplicationUser definition to your DAL.
  2. Inherit your MyDbContext from IdentityDbContext<ApplicationUser> or IdentityDbContext
  3. OnModelCreating - Provide the foreign key info.
  4. Pass MyDbContext while creating the UserManager<ApplicationUser>

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

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