我有这个查询,其结果实现了IEnumerable
。
RoutesEntities routesModel = new RoutesEntities();
LocalesEntities localesModel = new LocalesEntities();
var routesQuery = from rs in routesModel.Routes.ToList()
join ls in localesModel.Locales.ToList()
on rs.LocaleID equals ls.LocaleID
select new
{
LocaleID = rs.LocaleID,
RouteName = rs.RouteName
};
问题:如何从此查询中生成SQL,或如何将其类型转换为ObjectQuery
以使用routesQuery.ToTraceString()
?有可能吗?
原因:我想将所有SQL查询记录到DB,但我不想为连接的上下文(表)创建新对象
重要:我最初无法使用ObjectQuery
因为我毕竟使用了
ListView.DataSource = routesQuery;
ListView.DataBind()
它可能导致在一个数据源上使用不同的上下文(DB表)时出错。
那么我该怎么做才能生成SQL查询?
提前致谢。
答案 0 :(得分:1)
var routesQuery = from rs in routesModel.Routes.ToList()
join ls in localesModel.Locales.ToList()
设置此查询的方式是,将整个Routes表加载到内存中,并将整个Locales表加载到内存中。您的查询(有两个),看起来像:
select fieldlist
from tablename
您应该将两个查询分成单独的ObjectQuery实例,并单独记录它们。但更重要的是,你可能不应该将未经过滤的表格内容读入内存!
答案 1 :(得分:0)
您违反两个不同的数据上下文,因此您将对每个上下文运行不同的查询并将结果加入客户端。对于LINQ-to-SQL上下文,数据库中的查询可以记录为Femaref已经指出的。 EF ObjectContext
仍有工作要做,以赶上LINQ-to-SQL并且不提供日志记录。但是ObjectQuery.CommandText
会返回查询文本。无法记录客户端中的连接,因为它不存在。它不是SQL查询,是LINQ-to-Objects循环。
答案 2 :(得分:0)
有答案:
http://arteinvolo.org.ua/2010/09/21/how-to-use-entity-framework-with-multiple-entities/
public static EntityConnection setSchema(string[] edmxFiles)
{
XNamespace edmxns = "http://schemas.microsoft.com/ado/2007/06/edmx";
XNamespace edmns = "http://schemas.microsoft.com/ado/2006/04/edm";
XmlDocument edmxXml = new XmlDocument();
XmlNamespaceManager nameSpace = new XmlNamespaceManager(edmxXml.NameTable);
List<XmlReader> ssdlReader = new List<XmlReader>();
List<XmlReader> csdlReader = new List<XmlReader>();
List<XmlReader> mslReader = new List<XmlReader>();
foreach (string edmxFile in edmxFiles)
{
edmxXml.Load(edmxFile);
nameSpace.AddNamespace("edmx", edmxns.NamespaceName);
XmlNode ssdlNode = edmxXml.SelectSingleNode("//edmx:StorageModels", nameSpace);
XmlNode csdlNode = edmxXml.SelectSingleNode("//edmx:ConceptualModels", nameSpace);
XmlNode mslNode = edmxXml.SelectSingleNode("//edmx:Mappings", nameSpace);
ssdlReader.Add(XmlReader.Create(new StringReader(ssdlNode.InnerXml)));
csdlReader.Add(XmlReader.Create(new StringReader(csdlNode.InnerXml)));
mslReader.Add(XmlReader.Create(new StringReader(mslNode.InnerXml)));
}
StoreItemCollection storageCollection = new StoreItemCollection(ssdlReader);
EdmItemCollection edmCollection = new EdmItemCollection(csdlReader);
StorageMappingItemCollection mappingCollection = new StorageMappingItemCollection(edmCollection, storageCollection, mslReader);
MetadataWorkspace workSpace = new MetadataWorkspace();
workSpace.RegisterItemCollection(storageCollection);
workSpace.RegisterItemCollection(edmCollection);
workSpace.RegisterItemCollection(mappingCollection);
SqlConnection metaConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomSqlConnection"].ToString());
EntityConnection entityConnect = new EntityConnection(workSpace, metaConnect);
return entityConnect;
}
使用方法:
EntityConnection entityConnection = Connector.setSchema(new string[] {
Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "/App_Model/RoutesModel.edmx"),
Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "/App_Model/LocalesModel.edmx")
});
entityConnection.Open();
String queryData = "SELECT rs.RouteID, rs.RouteURL, ls.LocaleName, ls.IsActive, ls.LocaleDescription FROM RoutesEntities.Routes AS rs INNER JOIN LocalesEntities.Locales AS ls ON ls.LocaleID = rs.LocaleID";
EntityCommand queryCommand = new EntityCommand(queryData, entityConnection);
DbDataReader routesReader = queryCommand.ExecuteReader(CommandBehavior.SequentialAccess);
ListView.DataSource = routesReader;
ListView.DataBind();