Can code-first Entity Framework do cross database queries with SQL Server DBs on the same box?

后端 未结 4 1900
终归单人心
终归单人心 2020-12-31 09:06

I know there have been a lot of questions about Entity Framework doing cross database queries on the same server posted to stackoverflow. Mostly the answer seems to be \'no\

4条回答
  •  情歌与酒
    2020-12-31 10:01

    There are two ways to do it.

    One is, of course, to create a view in one of the databases which does the cross database query, then access the veiw from your model as you would any other view.

    The other was it to create the same cross database query view within the model itself by creating a DefiningQuery. This is most similar to how you would do it with SQLClient. In SQLClient, you'd create the view in T-SQL as the text of a SQLCommand, then execute the command to create a data reader or data table. Here you use the same T-SQL to create a DefiningQuery, then link it up with an Entity that you create manually. It's a bit of work, but it does exactly what you'd want it to.

    Here's a link on using DefiningQuerys: http://msdn.microsoft.com/en-us/library/cc982038.aspx.

    If you happen to have the book "Programming Entity Framework" by Lerman from O'Reilly, there a good example in chapter 16.

    So you have to jump through a few hoops to do what you used to do directly with SQLClient, BUT you get the modeled Entity.

提交回复
热议问题