使用实体框架SqlQuery填充子属性 [英] Populating child property with Entity Framework SqlQuery
问题描述
给出以下POCO代码第一实体
Given the following POCO Code First Entities
public class Customer
{
public int CustomerId { get; set; }
public string CustomerTitle { get; set; }
public string CustomerFirstName { get; set; }
public string CustomerLastName { get; set; }
public ICollection<Order> Orders { get; set; }
}
public class Order
{
public int OrderId { get; set; }
...
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
使用linq,您可以使用Include属性来填写订单,如下所示:
Using linq you can get the Orders filled in by using the Include property as in
var cust = (from cust in context.Customer
where cust.CustomerId == 1
select cust)
.Include(ord => ord.Orders)
.FirstOrDefault();
我正在尝试使用paramaterised sql来获得相同的结果,
I am trying to get the same result using paramaterised sql, using
Customer co = context.Customer.SqlQuery(
@"select [Customer].[CustomerId],
...
[Order].[OrderId] AS [OrderId],
...
from Customer join Order on Customer.CustomerId = Order.CustomerId where Customer.CustomerId = @custid", sqlParm)
.FirstOrDefault();
如何使用上面的命令填充co.Orders中的Orders,看来我无法在SqlQuery中使用Include语句.这是一个非常简化的示例,仅用于说明目的,实际查询将更加复杂.
How do I get the Orders in co.Orders to be populated with the above command, it seems like I can't use the Include statement with SqlQuery. This is a very simplified example for illustrative purposes only, the actual queries will be more involved.
推荐答案
这是完全不可能的.直接SQL执行不提供导航属性的填充,并且您实际上不能使用Include.您必须执行两个单独的SQL查询才能获取Cutomer
和她的Orders
.
This is not possible at all. Direct SQL execution doesn't offer filling of navigation properties and you really can't use Include. You must execute two separate SQL queries to get Cutomer
and her Orders
.
这篇关于使用实体框架SqlQuery填充子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!