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

后端 未结 4 1869
终归单人心
终归单人心 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

    0 讨论(0)
  • 2020-12-31 09:53

    The answer is still the same. If you want to execute cross database query you have to fall back to SQL and use SqlQuery on context.Database.

    0 讨论(0)
  • 2020-12-31 09:53

    Warning! using DefiningQuerys can be VERY SLOW!

    Here's an example:

    If this is the defining query that you create an Entity against:

    Select
        C.CustomerID,
        C.FirstName,
        C.LastName,
        G.SalesCatetory
    From
        CustomerDatabase.dbo.Customers C
        Inner Join MarketingDatabase.dbo.CustomerCategories G on G.CustomerID = C.CustomerID
    

    Then when you do a select against the Entity by CustomerID, the SQL trace looks something like this:

    Select
    [Extent1].[CustomerID] as [CustomerID],
    [Extent1].[FirstName] as [FirstName],
    [Extent1].[LastName] as [LastName],
    [Extent1].[SalesCatetory] as [SalesCatetory]
    From (
            Select
                C.CustomerID,
                C.FirstName,
                C.LastName,
                G.SalesCatetory
            From
                CustomerDatabase.dbo.Customers C
                Inner Join MarketingDatabase.dbo.CustomerCategories G on G.CustomerID = C.CustomerID
            ) as [Extent1]
    Where '123456' = [Extent1].[CustomerID]
    

    SQL Server may run this query very slowly. I had one case, a little more complicated than the above example, where I tried the DefiningQuery text directly in a SQl Server Management Console query window by adding a where clause for the value I wanted to select for. It run in less than a second. Then I captured the SQL Trace from selecting for the same value from the Entity created for this DefiningQuery and ran the SQL Trace query in a SQL Server query window - it took 13 seconds!

    So I guess that only real way to do cross database queries is to create a veiw on the server.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题