I have two Objects (UserProfile and Login) that resides in different assemblies and I have to create a relationship between them in SQL Server (I don't care about the DbContext relationship, but I'd like to assure the database referential integrity between the their respective tables).
The easiest way is to update the database (I am using code first) and run a alter table to create the relationship however I think this approach is kind of dirty (even if using SqlResource in Up method).
I tried unsuccessfully to create this relationship using HasOptional and Map methods (fluent api says that ProfileId isn't a reference type when I try something like "HasOptional(a => a.IdUsuario)...").
// UserContext's Classes (Assembly 'A')
public class UserProfile
{
public int Id { get; set; } // PK
// some properties omitted
}
public class UserContext : DbContext
{
DbSet<UserProfile> UsersProfiles { get; set; }
}
// SecurityContext's Classes (Assembly 'B')
public class Login
{
public int Id { get; set; }
public string UserName { get; set; }
public int ProfileId { get; set; } // UserProfile's FK
}
public class SecurityContext : DbContext
{
DbSet<Login> Logins { get; set; }
}
I tried to reference both classes, include navigation properties and use "modelBuilder.Ignore<>" to select which class I want to really synchronize in each context, but this ends in a circular reference problem.
I receive the message "The navigation property 'UserProfile' is not a declared property on type 'Login'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property." if try to create fake classes just to mapping purposes.
Am I missing something?