Im new to EF Code First and have been enjoying it for weeks now. Im faced with dealing with 2 databases (both in the same Sqlserver). The first database contains 1 table that I just have to reference for its Primary Key, which is 'logically' related to tables I created (2nd DbContext -- 2nd database). Based on this key, I will be querying my 2nd DbContext tables (the actual concerned tables for the mvc site). By the way, Customer has 0..* CustomerUsers.
First dbcontext:
public class Customer
{
[StringLength(10, MinimumLength = 3), Required]
public string CompanyID { get; set; }
[StringLength(8, MinimumLength = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string CustomerID { get; set; }
[StringLength(500)]
public string CustomerName { get; set; }
//*Initially, under the same dbContext, they were related, but now am separating
//them because of physical database separation; hence the navigation property
//public virtual ICollection<CustomerUser> CustomerUsers { get; set; }
}
In the OnModelCreating method:
modelBuilder.Entity<Customer>()
.HasKey(c => new { c.CompanyID, c.CustomerID});
The 'logically' related entity
public class CustomerUser
{
[Display(Name = "Customer ID")]
public int CustomerUserID { get; set; }
//[ForeignKey("Customer")]
[Display(Name = "CompanyID"), Required]
[StringLength(10, MinimumLength = 3)]
public string CompanyID { get; set; }
//[ForeignKey("Customer")]
[Display(Name = "CustomerID"), Required]
[StringLength(8, MinimumLength = 3)]
public string CustomerID { get; set; }
[StringLength(50), Display(Name = "First Name"), Required]
public string FirstName { get; set; }
[StringLength(50), Display(Name = "Last Name"), Required]
public string LastName { get; set; }
//*Initially related thru foreign key to Customer but separation of database
//thus no more relations – just a querying of Customer’s id from other database
//public virtual Customer Customer { get; set; }
}
OnModelCreating method:
modelBuilder.Entity<CustomerUser>()
.HasKey(c => new { c.CustomerUserID });
modelBuilder.Entity<CustomerUser>()
.HasRequired(p => p.Customer)
.WithMany(c => c.CustomerUsers)
.HasForeignKey(p => new {p.CompanyID, p.CustomerID});
How and where do i declare my DbContexts? Currently I have an assembly that is referenced by the web app. Also, how would this pan out for my deployment? That one table used for just referencing its key is already existing, while the rest of the tables, I will have yet to create in SqlServer(they're existing in my VS2012 Server Explorer).
Would there be any deployment-related issues I could run into with this design?