3

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?

1 Answers1

0

How and where do i declare my DbContexts?

BIG question. Data Access Layer is typical. Not in the Core/Domain layer. That couples the core to EF.

Currently I have an assembly that is referenced by the web app. Also, how would this pan out for my deployment?

If you dont use CORE/DOMAIN Layer + Data access Layer + UI Layer the the development will be tightly coupled to Data access tool.

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?

No, multiple context multiple DB access from one solution is no issue for EF. Although the way you instantiate the context needs to be managed carefully. eg https://stackoverflow.com/a/20118259/1347784

Community
  • 1
  • 1
phil soady
  • 11,043
  • 5
  • 50
  • 95
  • Hi Phil, thanks for your reply. As you mentioned pertaining to EF not having any issues for multiple DB access (I being new to EF, my understanding is that each DB access is represented by a DbContext?). Does it mean that I can have 2 connection strings in my web.config such as: (above connection strings will be pointing to actual databases in deployed environment) – user3047862 Nov 29 '13 at 08:43
  • yes you can have multiple connection strings, or you can pass connection information to DbContent in constructor – phil soady Nov 30 '13 at 22:21