Question

I have an issue that I have been struggling with. I would appreciate any help

So, I have an application that uses Code first. For authentication, I use Asp.Net Identity 2.0.1. So I have a User class that derives from Identity User like this:

public class User: IdentityUser
{
    public virtual UserInfo UserInfo { get; set; }
    public virtual Organization Organization { get; set; }
}

Then i define my Context as:

public class IdentityContext: IdentityDbContext<User>
{
    public IdentityContext(): base("Name=IdentityContext")
    {
    }

    static IdentityContext()
    {
        Database.SetInitializer<IdentityContext>(new CreateDatabaseIfNotExists<IdentityContext>());
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new UserMap());
        modelBuilder.Configurations.Add(new UserInfoMap());
    }
}

Then i enable migrations using this context. And since Organization class is linked to User it also is added to the migration. But I dont want that, since its different from the Identity classes. My Organization class is as follows:

public partial class Organization: EntityBase
{
    public Organization()
    {
        this.Users = new List<User>();
    }
    public int OrganizationId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

This derives from EntityBase and not from IdentityUser class and is handled differently.

What I would be able to do is to handle all my non-identity classes through a different context, lets say AppContext that could be defined as:

public class AppContext: DbContextBase
{
    public AppContext(): base("Name=IdentityContext")
    {
    }

    static AppContext()
    {
        Database.SetInitializer<AppContext>(new CreateDatabaseIfNotExists<AppContext>());
    }

    public new IDbSet<T> Set<T>() where T : class
    {
        return base.Set<T>();
    }

    protected override void OnModelCreating(DbModelBuilder builder)
    {
        builder.Configurations.Add(new OrganizationMap());
    }
}

And then enable a second migration for this context.

But when I enable a second migration it fails with the exception that "Migrations have already been enabled in project 'Datastore'. To overwrite the existing migrations configuration, use the -Force parameter.". Although it still creates the Configuration file for the second migration.

So when i try to add a migration for this configuration it complains the following:

One or more validation errors were detected during model generation:

Datastore.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

Datastore.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

Datastore.UserInfo: : EntityType 'UserInfo' has no key defined. Define the key for this EntityType.

IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type >'IdentityUserLogin' that has no> keys defined.

IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type >'IdentityUserRole' that has no keys defined.

UserInfoes: EntityType: EntitySet 'UserInfoes' is based on type 'UserInfo' that has no >keys defined.

I am not sure how to get around this? Any help is really appreciated

Was it helpful?

Solution

The idea was to inherit the AppContext with IdentityDbContext. IdentityDbContext itself inherits from DbContext so it has all the functionality available for DbContext plus extra configuration requirements for the Asp.Net Identity framework

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top