EF Create an additional table
Using EF6, I created a simple POCO account named Account to track user data:
public class Account
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//Others
}
This Account object differs from the following DbContext (removed for brevity):
public class MyContext : DbContext
{
//constructor
//other DBSets
public DbSet<Account> Accounts {get;set;}
}
OnModelCreating removes PluralTableNamesConvention, so it successfully creates a table named "Account" with correct properties.
I also have ApplicationDbContext which contains the following definition for ApplicationUser:
public class ApplicationUser : IdentityUser
{
//GenerateUserIdentityAsync method....
public virtual Account AccountInfo { get; set; }
}
The goal is to be able to create a user and then make calls like
myStuff.SearchByName(user.AccountInfo.FirstName)
Problem: When I create a user and EF generates ASP.net identity tables, it also creates a table named "Accounts". All users created their information sent to the Accounts table, not the original Account table that I intended to use.
I'm not sure how to get IdentityDbContext to only use the original Account table and not generate it. I tried:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>().ToTable("Account");
base.OnModelCreating(modelBuilder);
}
but it returns the following. Any help would be appreciated :)
Model compatibility cannot be verified because the database does not contain model metadata. Model compatibility can only be checked for databases created using First Code or First First Code.
source to share
So, not sure if this will be the correct answer or not. But I created an MVC4 / EF5 project a while ago and had to do something similar. I used the WebSecurity Identity Framework or whatever it was called (think there is a different identity system now). Basically, all I had to do was put a few lines of code in the Application_Start method for Global.ascx.
// Tells membership provider to use the Baller table too store user profile information
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("BallSoHardContext", "Baller", "Id", "UserName", autoCreateTables: false);
}
So I'm just telling him to use the "Baller" table to store information. You may be using a different identification system as you are using EF6, but you can find something similar in the API.
source to share