0

I'm using EntityFramework 6. I would like to split my DbContext into multiple smaller ones. Edit: I have a single database I wish to connect to.

I will have a single DbContext with all the entities and migrations that only one central application will use. Other applications will use the smaller DbContext without migrations.

I have found that I need to inherit all DbContext from IdentityDbContext, even if they use none of it.

internal class SmallerDbContext : IdentityDbContext<ApplicationUser, 
ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public DbContext<Entity1> NotInSmallerDbContext { get; set; }
    public DbContext<Entity1> InSmallerDbContext { get; set; }
}

I would like this to be:

internal class SmallerDbContext : System.Data.Entity.DbContext
{
    public DbContext<Entity1> InSmallerDbContext { get; set; }
}

But this yields the following errors:

Paris.DAL.Repo.DbContext.ApplicationUserLogin: : EntityType 'ApplicationUserLogin' has no key defined. Define the key for this EntityType.
Paris.DAL.Repo.DbContext.ApplicationUserRole: : EntityType 'ApplicationUserRole' has no key defined. Define the key for this EntityType.
ApplicationUserLogins: EntityType: EntitySet 'ApplicationUserLogins' is based on type 'ApplicationUserLogin' that has no keys defined.
ApplicationUserRoles: EntityType: EntitySet 'ApplicationUserRoles' is based on type 'ApplicationUserRole' that has no keys defined.

I have tried to add these tables to the DbContext, but to no avail.

public IDbSet<ApplicationUser> Users { get; set; }
public IDbSet<ApplicationRole> Roles { get; set; }

Is there any way I can create DbContext that does not inherit from IdentityDbContext?

Diana
  • 449
  • 4
  • 13
  • Have a look at this post's answer: https://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea – Dimitri Mar 20 '19 at 08:58
  • Some examples of using multiple contexts: https://www.tutorialspoint.com/entity_framework/entity_framework_multiple_dbcontext.htm and here as well: https://msdn.microsoft.com/en-us/magazine/dn948104.aspx – Dimitri Mar 20 '19 at 09:09
  • The suggested links don't answer my question, I'm afraid – Diana Mar 22 '19 at 09:22
  • Do you need to use multiple databases or just one? – Dimitri Mar 27 '19 at 08:57
  • @Dimitri just one database, but multiple DbContext on that database – Diana Mar 28 '19 at 17:05
  • Does Entity1 reference the other context (for example User)? – Steve Greene Mar 28 '19 at 19:06
  • Some of the entities do reference User, yes. With a foreign key relationship, most often, where User <-> Entity1 is a one-to-many relationship. – Diana Mar 29 '19 at 08:33

1 Answers1

1

I was on the right path by adding the objects to the DbContext. You need to add only two, the User and UserLogin. The others are not needed.

    public DbSet<ApplicationUser> Users { get; set; }
    public DbSet<ApplicationUserLogin> UserLogins { get; set; }

Additionally, you need to set the correct table for both objects and you need to add some of virtual properties so that Entity Framework can handle these types as it would any of your other classes.

[Table("AspNetUsers")]
public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
}


[Table("AspNetUserLogins")]
public class ApplicationUserLogin : IdentityUserLogin<int>
{
    [Key, Column(Order = 0)]
    public override string LoginProvider { get; set; }

    [Key, Column(Order = 1)]
    public override string ProviderKey { get; set; }

    [Key, Column(Order = 2)]
    public override int UserId { get; set; }
}

Now, your DbContext no longer has to inherit from IdentityDbContext.

Diana
  • 449
  • 4
  • 13