0

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.

Lee Stevens
  • 472
  • 5
  • 17
  • 1
    Possible duplicate of [Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?](https://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea) – Kahbazi Apr 20 '18 at 09:10

1 Answers1

0

Traditionally in a situation like this I would rather you make your Context inside of the DataAccessLayer be the IdentityContext. Since you have ties between the IdentityContext and that particular context anyway, it gives you a single context to represent your application and doesn't introduce a shared context issue.

This would result in removing the DBContext from your web project, and changing the base class of your Context inside of the DataAccessLayer to become IdentityDbContext<ApplicationUser> rather than the current DbContext base class.

Most likely, this will result in you moving a few entities into the lower DLL, but again it makes that DLL more representative of the entire solution.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173