문제

If we want to map a model(class) to a particular table(in a database) it can be done using [table] attribute,but it will be only confined to one database. What if we have say 4 classes and 2 databases.I want to map class1 to table1(DB1) ,class2 to table2(DB1) , class3 to table3(DB2) and again class4 to table4(DB2) . Since there are 2 databases we are dealing with ,will we have to create different data contexts or is there a different approach?

도움이 되었습니까?

해결책

I don't see how you could get around (and why you would want to get around) using one context per DB since using two databases also requires two connection strings.

You mention the table attribute, are you using code-first (the question is tagged with database-first)? In database-first approach you would just use the wizard to create an additional context, in code-first you would use something like this (along with DB1Connection / DB2Connection connection strings in your config):

public class DB1Entities: DbContext {
    public DB1Entities() : base("DB1Connection") {
    }

    public DbSet<Table1> Table1 { get; set; }
    public DbSet<Table2> Table2 { get; set; }
}

public class DB2Entities: DbContext {
    public DB2Entities() : base("DB2Connection") {
    }

    public DbSet<Table3> Table3 { get; set; }
    public DbSet<Table4> Table4 { get; set; }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top