I'm using EF6 Code First.
I have a class library "IdentitySecurity" that contains a context "IdentityDBContext" that points to an "IdentitySecurity" database and the IdentityModels.
I then have my web project that contains a context "ProjectDBContext" that points to a "Project" database and the ProjectModels.
If I create a model in ProjectModels like this:
public class Project
{
public int ProjectId { get; set; }
public virtual IdentitySecurity.Models.ApplicationUser UpdateUser { get; set; }
}
It can't create a migration, I think it is because it can't make a foreign key relationship to another database and expects it to be in the Project database.
The error it gets when you try to create a new migration is this:
One or more validation errors were detected during model generation:
Project.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. Project.Models.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.
Another example I ran into similar to this is I had a config table that multiple applications use in a common database area. I attempted to reference this in a model like this:
public virtual Common.Models.CommonItem CommonItem { get; set; }
In this situation, it added the migration and updated the database without error, but what it did is actually create the table "CommonItem" in the Project Database instead of using the one in the Common database.
Is there a way to set up EF6 CodeFirst so I can have configuration tables or ASPIdentity tables in another database, and still reference them as virtual objects?
Thank you.
I'm afraid for foreign key references you'll have to move everything into one DB and have ProjectDbContext
to inherit from IdentityDbContext
.