entity framework navigation property to table in different db
I have 2 tables in different db
db1..Towns
id
Nazv
db2..开发者_StackOverflow社区MathchedTown
id
t_id
d_name
They are joined by по Towns.id = MathchedTown.t_id This EF classes:
[Table("Towns")]
public class Town
{
[Key]
public int id { get; set; }
public string Nazv { get; set; }
}
[Table("MathchedTown")]
public class mTown
{
[Key]
public int id { get; set; }
[Required]
public string t_id{ get; set; }
[Required]
public string d_name{ get; set; }
[ForeignKey("t_id")]
public virtual Town town { get; set; }
}
when i try to get item.town.nazv i get error: Invalid object name 'dbo.Towns'. If i change [Table("Towns")] to [Table("db1.dbo.Towns")], then appear almost the same error: Invalid object name 'dbo.db1.dbo.Towns'.
That all errors are SqlExceptions
How i can talk EF4 don't substite the "dbo." prefix?
Entity framework does not support multiple databases in a single context. But it supports multiple schemas in a single database. If you do not specify the schema it will assume dbo
.
[Table("Towns", "MySchema")]
public class Town
You can specify the schema as above.
If you want to use a table in a different database you can create a view to that table in your database. But it will be read only.
Unfortunately EF4 does not support navigation property that is in other db. You have two choices.
One, create a stored procedure and import it as a function in edm. And genetate complex type for the return result.
Two, create two entity models for each database. And run a query for a database then run the other query to the other database with where clause from the first query result.
精彩评论