Joining tables from two databases using entity framework

前端 未结 4 1624
心在旅途
心在旅途 2020-12-17 17:09

I am working on an ASP.NET MVC 4 web application. I am using Entity Framework as the data access layer, using database first approach (.edmx file).

Curr

4条回答
  •  眼角桃花
    2020-12-17 17:40

    If you want to do it with a single database call you will have to create a View in the database that joins the 2 tables from separate db's. Once the view is created you can add it to EF as a single object, which you can manipulate further and Query off of. The view will basically be a table and it will be easily maintable and easy to bind to a strongly typed model

    Another way ,similiar like you have posted, you can query separate .edmx files and then join them. Yes, there is 2 calls to the database but it shouldn't be that expensive and probably won't notice a difference.

    using(var db = new MyEntities())
    using (var db2 = new MyEntities2())
    {
       var one = db.Table1.AsEnumerable();
       var two = db2.Table2.AsEnumerable(); 
    
       var result = from o in one
                    join t in two on o.Id equals t.Id
                    // blah blah
    
    }
    

提交回复
热议问题