本文介绍了带有EF Core的类似Dapper的多查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用dapper,一次运行多个查询非常容易:
With dapper it is pretty easy to run mulitple queries in one run:
string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID; SELECT * FROM InvoiceItem WHERE InvoiceID = @InvoiceID;";
using (var connection = My.ConnectionFactory())
{
connection.Open();
using (var multi = connection.QueryMultiple(sql, new {InvoiceID = 1}))
{
var invoice = multi.Read<Invoice>().First();
var invoiceItems = multi.Read<InvoiceItem>().ToList();
}
}
如何使用EFCore归档?
How to archieve this with EFCore?
我找到了类似的解决方案
I found solutions like
var query = from _ in db.Invoices
select new {
Invoice = db.Invoices.Single(x=>x.InvoiceId == 1),
InvoiceItems = db.InvoiceItems.Where(x=>x.InvoiceId == 1),
}
但是这不起作用.我可以在配置文件中看到这在两个连接中运行两个查询.我什至可以看到连接没有被重用(为什么?).
but this does not work.I can see in the profiling this runs two queries in two connections. I can even see that the connection is not reused (why?).
如何使用EFCore运行多个查询,这是EFCore巨大的性能不足吗?谢谢.
How to run multiple queries with EFCore is this a big performance-lack of EFCore?Thanks.
推荐答案
问题是:MultiActiveResults为假.
The problem was: MultiActiveResults was false.
这篇关于带有EF Core的类似Dapper的多查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!