问题描述
我在一个单独的数据访问层类库项目中有一个 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!
推荐答案
- 将
ApplicationUser
定义移动到您的 DAL. - 从
IdentityDbContext
或IdentityDbContext
继承您的 - OnModelCreating - 提供外键信息.
- 在创建
UserManager
时传递
MyDbContext
MyDbContext
- Move the
ApplicationUser
definition to your DAL. - Inherit your
MyDbContext
fromIdentityDbContext<ApplicationUser>
orIdentityDbContext
- OnModelCreating - Provide the foreign key info.
- Pass
MyDbContext
while creating theUserManager<ApplicationUser>
这篇关于将 MyDbContext 与 IdentityDbContext 合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!