Enable migrations with reference to ApplicationUser identity framework code first

409 Views Asked by At

I'll create a database with identity framework (code first way) but I've errors when I run code below in the package manager.

PM> Enable-Migrations -ContextTypeName FreeTime.AspMvc.Context.FreeTimeContext -Force

Here are my errors:

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

  • IdentityUserRole: : EntityType IdentityUserRole 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.

More information:

More information (Click to see full screen)

In other classes I've made a property type of ApplicationUser named UserId and one of type of string named User for holding witch user has made witch topic. Here you've an example:

public class Topic 
{
    [Key]
    public int TopicId { get; set; }

    [ForeignKey("dbo.AspNetUsers.Id")]
    public string UserId { get; set; }

    public ApplicationUser User { get; set; }
}

Small update: Here is my context file:

public class FreeTimeContext: DbContext
{
    public DbSet<Topic> Topics { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }
}

I've also seen that there is a second context file named ApplicationDbContext.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("FreeTimeContext" /* --> this I've edited into my own name */,
               throwIfV1Schema: false)
    {
    }

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

Big update: In a later state I've tried other things like list below:

  1. First I've tried to enabled the migrations for ApplicationDbContext. Here are the steps I've done.

    • First I've run this code:

      PM> enable-migrations -ContextTypeName FreeTime.AspMvc.Models.ApplicationDbContext -Force

      This is done without errors.

    • Then I've run this:

      PM> add-migration "initAspTables"

      Also no problems with this code.

    • Finaly I've update my database:

      PM> Update-Database

      Also done with no problems.

    • Check if the tables are created into the server manager → Check but doesn't contain my own tables. ☹

      Object explorer from server manager with my database and the tables.

    Conclusion: not really what I will have.

  2. Second thing I've done is inherit FreeTimeContext from ApplicationContext instead of DbContext like code below.

    public class FreeTimeContext: ApplicationDbContext
    {
        public DbSet Topics { get; set; }
    
        public FreeTimeContext(): base() 
        { }
        // other code
    }

    And again to try enable the migrations with this code:

    PM> enable-migrations -ContextTypeName FreeTime.AspMvc.Context.FreeTimeContext -Force

    but I've this error:

    The ForeignKeyAttribute on property UserId on type FreeTime.AspMvc.Models.Topic is not valid. The navigation property dbo.AspNetUsers.Id was not found on the dependent type FreeTime.AspMvc.Models.Topic. The Name value should be a valid navigation property name.

    More information:

    More information(Click to see full screen)

    Conclusion: didn't work.

Can you help me? I don't know what else I could do to migrate the class ApplicationUser together with my own classes to the database.

0

There are 0 best solutions below