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

问题描述

我在一个单独的数据访问层类库项目中有一个 MyDbContext.我有一个带有默认 IdentityDbContext 的 ASP.NET MVC 5 项目.这两个上下文使用相同的数据库,我想使用 AspNetUsers 表作为我的一些表的外键.所以我想合并这两个Context,我也想使用ASP.NET Identity.

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.

我该怎么做?

请多多指教,

这是我合并后的上下文:

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>();
    }
}

这是我的 RepositoryBase 类:

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");
             }
         }
     }

'System.Data.Entity.ModelConfiguration.ModelValidationException' 类型的异常发生在 EntityFramework.dll 中,但未在用户代码中处理

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:

更新:我找到了解决方案.

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

I had to remove this naming convention:

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

并将 ef cf 模型迁移到 db,以使用 asp.net 身份的命名约定重命名我的表.它现在可以工作了!

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. ApplicationUser 定义移动到您的 DAL.
  2. IdentityDbContextIdentityDbContext
  3. 继承您的 MyDbContext
  4. OnModelCreating - 提供外键信息.
  5. 在创建UserManager
  6. 时传递MyDbContext
  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 合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:05