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 IdentityUserLoginhas no key defined. Define the key for thisEntityType.
IdentityUserRole: : EntityType IdentityUserRolehas no key defined. Define the key for thisEntityType.
IdentityUserLogins: EntityType: EntitySet IdentityUserLoginsis based on typeIdentityUserLoginthat has no keys defined.
IdentityUserRoles: EntityType: EntitySet IdentityUserRolesis based on typeIdentityUserRolethat has no keys defined.
More information:
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:
-
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. ☹
Conclusion: not really what I will have.
-
Second thing I've done is inherit
FreeTimeContextfromApplicationContextinstead ofDbContextlike 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
ForeignKeyAttributeon propertyUserIdon typeFreeTime.AspMvc.Models.Topicis not valid. The navigation propertydbo.AspNetUsers.Idwas not found on the dependent typeFreeTime.AspMvc.Models.Topic. The Name value should be a valid navigation property name.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.
