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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…