I currently have multiple projects with multiple DbContexts:
Projects (DbContext):
- Model
- DataAccessLayer (EntityContext)
- ServiceLayer
- Web (IdentityContext)
I have a Model in the Model's project called Department that is created by the EntityContext and i reference to it in the ApplicationUser.cs in the Web Project.
When the IdentityContext is trying to create the AspNet Tables it also tries to recreate the Department Table and i get the following error:
"There is already an object named 'Departments' in the database."
Here's my Identity Files:
The IdentityContext is my Web Project:
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("EntityContext", throwIfV1Schema: false)
{
Configuration.LazyLoadingEnabled = false;
Configuration.AutoDetectChangesEnabled = false;
Configuration.ProxyCreationEnabled = false;
Configuration.ValidateOnSaveEnabled = false;
}
static ApplicationDbContext()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, ApplicationDbInitializer>());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
ApplicationDbInitializer.cs
public class ApplicationDbInitializer : DbMigrationsConfiguration<ApplicationDbContext>
{
public ApplicationDbInitializer()
{
AutomaticMigrationsEnabled = false;
#if DEBUG
AutomaticMigrationDataLossAllowed = true;
AutomaticMigrationsEnabled = true;
#endif
}
protected override void Seed(ApplicationDbContext context)
{
base.Seed(context);
}
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
/// <summary>
/// Linked Department Id
/// </summary>
public Guid? DepartmentId { get; set; }
/// <summary>
/// Linked Department Object
/// </summary>
[ForeignKey("DepartmentId")]
public virtual Department Department { get; set; }
}
I then commented out the "public DbSet Department { get; set; }" in the EntityContext and now it builds but when i try to access it i get this error:
"The entity type Department is not part of the model for the current context."
I understand both errors, but i can't figure out how to get them to be on the same context, as they are in different projects.