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

后端 未结 4 1916
终归单人心
终归单人心 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 09:50

    I think that the answer is still no, but there are ways around it.

    The reason why it is no, it that EF uses a DBContext, and a context has a connection string, and a connection string goes to a database.

    Here are 2 ways around it:

    • use 2 different contexts one against each database, this will mean bringing data to the client and merging it on the client.
    • use linked tables on the database, pulling data through views, so that EF sees it as coming from a single database.

    In your code it looks like you are using 2 dbcontexts

提交回复
热议问题