3

In our project we have a DbContext for our code first database which is working fine. I've created a second DbContext (AppDbContext) to encapsulate login information. It will point to the same database, but will not need to see all the tables, or migrate.

When the Identity system initializes this context in my user manager:

 var manager = new MkpUserManager(new UserStore<MkpUser, MkpRole, Guid, MkpLogin, MkpUserRole, MkpClaim>(context.Get<AppDbContext>()));

I get an error that:

Unable to determine the principal end of an association between the types 'PublicationSystem.Model.Organization' and 'PublicationSystem.Model.Resource'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Those two tables should not be handled by the AppDbContext. How can I get it to ignore them? Is having two DbContext files even practical?

Here's what AppDbContext looks like:

public class AppDbContext : IdentityDbContext<MkpUser, MkpRole, Guid, MkpLogin, MkpUserRole, MkpClaim>
{
    #region Constructors

    public AppDbContext()
        : base("IdentityConnection")
    {
    }

    #endregion

    #region Methods

    public static AppDbContext Create()
    {
        return new AppDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // Map Entities to their tables.
        modelBuilder.Entity<MkpUser>().ToTable("User");
        modelBuilder.Entity<MkpRole>().ToTable("Role");
        modelBuilder.Entity<MkpClaim>().ToTable("UserClaim");
        modelBuilder.Entity<MkpLogin>().ToTable("UserLogin");
        modelBuilder.Entity<MkpUserRole>().ToTable("UserRole");
        // Set AutoIncrement-Properties
        modelBuilder.Entity<MkpUser>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<MkpClaim>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<MkpRole>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        // Override some column mappings that do not match our default
        modelBuilder.Entity<MkpUser>().Property(r => r.UserName).HasColumnName("Email");
        modelBuilder.Entity<MkpUser>().Property(r => r.PasswordHash).HasColumnName("Password");
    }

    #endregion
}
2
  • A shot in the dark, are those classes (Organization and Resource) referenced by any of the Entities you're trying to include?
    – Vlad274
    Jul 9, 2015 at 17:45
  • No. So far they are not. Jul 9, 2015 at 18:48

1 Answer 1

2

You can have several contexts per project/solution without any issue. I do it often. However I think it is not a good idea to point two contexts to the same database.

You'll run into issues : - Who is responsible for updating the Schema of the DB ? - If you use Code-First Migrations, there'll only be one _MigrationHistory Table that the two DB will have to use. Weird issues ahead in my opinion.

I think that by pointing two context to the same DB you are 1/ taking a lot of unnecessary risks, and 2/ not following the intended usage of EF. With ASP.NET identity, the recommended way to do it, seems to have a separate DB (or they call it a Store) to manage authentication.

You would have one store for authentication (this is where you would store the password of your users), and you would have one DB for authorization also containing all the business data. The link between the two can be the email address for example.

You lookup the user in the Authentication DB, and if it's ok you log him in and use your main DB for the common business tasks. To quickly fix your design, simply change the connection string of the DB containing the login information.

2
  • 1
    I'd be curious to see am example of how that works with two DBs. I had also thought of simply including the Identity tables in the same DbContext as the rest of the DB. I wanted to map Identity to our existing schema. However, we have user data stored in multiple tables, and no example I can find for Identity deals with that scenario. Jul 9, 2015 at 18:43
  • I agree with you, I was in hunt of an example of ASP.NET Identity used with a single DB too but couldn't find one. So I assumed that with all this OWIN authentication trend,the new standard was to decouple the authentication mecanism. But I completely understand what you're trying to do and why it is absolutely relevant to be honest. I'll edit my answer to provide an example
    – tobiak777
    Jul 10, 2015 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.