Error: “The specified LINQ expression contains references to queries that are associated with different contexts”

后端 未结 3 1190
眼角桃花
眼角桃花 2020-11-27 17:26

I am receiving the error shown in the title from a LINQ query that includes two tables from two different edmx files. Here is the query:

var query = (from a          


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 17:49

    You have to create manually EntityConnection filled with resources from all .EDMXs you want to use. You can do it either by adding connection to app.config or programmaticaly. Then you can create DBContext using prepared EntityConnection.

    method a)

    
    
    using (EntityConnection oEntityConnection =
        new EntityConnection("name=MyConnection"))
    {
        using(DbContext oDBContext = new DbContext(oEntityConnection))
        {
            //your code - available are entities declared in Entities.ModuleA and Entities.ModuleB
        }
    }
    

    method b)

     using (EntityConnection oEntityConnection =
            new EntityConnection(new MetadataWorkspace(
            new string [] { 
    "res://Entities.ModuleA/", 
    "res://Entities.ModuleB/" 
    },
            new Assembly[] { 
    Assembly.GetAssembly(typeof(Entities.ModuleA.AnyType)),
    Assembly.GetAssembly(typeof(Entities.ModuleB.AnyType)) 
    }
            )))
        {
            using(DbContext oDBContext = new DbContext(oEntityConnection))
            {
                //your code - available are entities declared in Entities.ModuleA and Entities.ModuleB
            }
        }
    

提交回复
热议问题