Answered by:
EF 6 Context with Identity Context

Question
-
User-1526035670 posted
Hi
I have a class library which contains my DBContext (TestContext). Heres the code in this class
public class TestContext : DbContext { public TestContext() : base("name=TestProject") { } public virtual DbSet<Address> Addresses { get; set; } etc protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Address>() .HasMany(e => e.Companies) .WithRequired(e => e.Address) .WillCascadeOnDelete(false); } }
I now add Identity and move the classes into another class library so my models are sitting separately.
All works with the database i specified in the web.config against the entry TestProject (when i run the project the Identity tables were created and user was added) until i add another custom property to store the users details (my assumption is this would be added to the ASPNetUsers table)
public class ApplicationUser : IdentityUser { public string CustomField { get; set; } // THIS WOULD SHOW IN THE USERS TABLE public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("TestProject", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } }
So far all is the default code. I now run Add-Migration NewFieldAdded but receive the errors
One or more validation errors were detected during model generation:
Infrastructure.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
Infrastructure.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.Noted this thread https://stackoverflow.com/questions/28531201/entitytype-identityuserlogin-has-no-key-defined-define-the-key-for-this-entit
Attempted to make the changes, installed Microsoft.AspNet.Identity.EntityFramework to tweak the context but either i get the same error or everything compiles but the customField is never added to the table.
Any direction where im going wrong here? I think its the way i have the context and i would imagine having one Context should suffice but cant tell where im going wrong?
Thanks
Saturday, October 10, 2020 1:14 PM
Answers
-
User-1526035670 posted
After making the above change i still had problems similar to
- The column didnt exist in SQL but a property was under the ApplicationUser class. No updates were carried across.
- The column didnt exist in SQL but the error of the table existing.
Eventually i decided to delete the database and all the migrations. I allowed the DB to created via code and that did the trick. I dont know if there is a recommended approach or a work-around but if anyone has done this with a better experience, would be great to see what you did to have a project with Identity and EF6
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 12, 2020 1:58 PM
All replies
-
User1535942433 posted
Hi JamieP1,
As the article's solutions,there are three possible solutions:
1.Use separate DbContexts against two different databases or the same database but different tables.
2.Merge your DXContext with ApplicationDbContext and use one database.3
3.Use separate DbContexts against the same table and manage their migrations accordingly.
Best regards,
Yijing Sun
Monday, October 12, 2020 7:52 AM -
User-1526035670 posted
Hi
Thats a great idea :-) but i am going for option 2 but not sure what im doing wrong?
Will give this another go.
Edit 1: I added IdentityDbContext to inherit in my Context file (not the ApplicationDbContext). Amended the constructor. Changed the OnModelCreating as below
protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder);
Ran the Add-Migration command and i see all the tables/columns in the up method. I run Update-Database and receive "There is already an object named 'Addresses' in the database."
Monday, October 12, 2020 8:27 AM -
User-1526035670 posted
After making the above change i still had problems similar to
- The column didnt exist in SQL but a property was under the ApplicationUser class. No updates were carried across.
- The column didnt exist in SQL but the error of the table existing.
Eventually i decided to delete the database and all the migrations. I allowed the DB to created via code and that did the trick. I dont know if there is a recommended approach or a work-around but if anyone has done this with a better experience, would be great to see what you did to have a project with Identity and EF6
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 12, 2020 1:58 PM