ASP.Net核心 - 急切加载存储过程而不是包含

时间:2018-05-31 15:17:50

标签: asp.net-core dbcontext eager-loading

有没有办法通过让存储过程返回一个结果集来实现预先加载,然后该结果集可以自动绑定到父数据对象及其链接的数据对象? 我目前正在尝试使用ASP.Net Core 2.0

示例:

对于这些数据对象:

public class Address 
{
    public int Id {get;set;}
    public string Street1 {get;set;}
    public string Street2 {get;set;}
    public string City {get;set;}
    public string Region {get;set;}
    public string PostalCode {get;set;}
    public string Country {get;set;}
}
public class Product
{
    public int Id {get;set;}
    public string Name {get;set;}
    public decimal Price {get;set;}  
}
public class Customer
{
    public int Id {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public Address ShippingAddress {get;set;}
    public Address BillingAddress {get;set;}
    public List<Product> Products {get;set;}
}

控制器可以拨打一个返回许多客户的电话,类似于:

var recordSet = dbContext.Set<Customer>().FromSql("EXEC dbo.[GetCustomerInfo] @TokenDtime={0}", tokenDtime);

其中dbo。[GetCustomerInfo]包含以下内容:

查询A - Bssic加入:

SELECT 
    c.[Id]           [CustomerId]                        ,
    c.[FirstName]    [CustomerFirstName]                 ,
    c.[LastName]     [CustomerLastName]                  ,
    sa.[Id]          [CustomerShippingAddressId]         ,
    sa.[Street1]     [CustomerShippingAddressStreet1]    ,
    sa.[Street2]     [CustomerShippingAddressStreet2]    ,
    sa.[City]        [CustomerShippingAddressCity]       ,
    sa.[PostalCode]  [CustomerShippingAddressPostalCode] ,
    sa.[Country]     [CustomerShippingAddressCountry]    ,
    sb.[Id]          [CustomerShippingAddressId]         ,
    sb.[Street1]     [CustomerShippingAddressStreet1]    ,
    sb.[Street2]     [CustomerShippingAddressStreet2]    ,
    sb.[City]        [CustomerShippingAddressCity]       ,
    sb.[PostalCode]  [CustomerShippingAddressPostalCode] ,
    sb.[Country]     [CustomerShippingAddressCountry]    ,
    p.[Id]           [ProductId]                         ,
    p.[Name]         [ProductName]                       ,
    p.[Price]        [ProductPrice]
FROM
    [Customer] c
    LEFT OUTER JOIN
    [Address] sa ON c.[ShippingAddressId] = sa.[Id]
    LEFT OUTER JOIN
    [Address] sb ON c.[BillingAddressId] = sb.[Id]
    LEFT OUTER JOIN
    [CustomerProduct] cp ON c.[Id] = cp.[CustomerId]
    LEFT OUTER JOIN
    [Product] p ON cp.[ProductId] = p.[Id]
WHERE
    c.[TokenDtime] >= @TokenDtime

查询B - 多个记录集:

SELECT 
    c.[Id]           [CustomerId]                        ,
    c.[FirstName]    [CustomerFirstName]                 ,
    c.[LastName]     [CustomerLastName]                  ,
    sa.[Id]          [CustomerShippingAddressId]         ,
    sa.[Street1]     [CustomerShippingAddressStreet1]    ,
    sa.[Street2]     [CustomerShippingAddressStreet2]    ,
    sa.[City]        [CustomerShippingAddressCity]       ,
    sa.[PostalCode]  [CustomerShippingAddressPostalCode] ,
    sa.[Country]     [CustomerShippingAddressCountry]    ,
    sb.[Id]          [CustomerBillingAddressId]          ,
    sb.[Street1]     [CustomerBillingAddressStreet1]     ,
    sb.[Street2]     [CustomerBillingAddressStreet2]     ,
    sb.[City]        [CustomerBillingAddressCity]        ,
    sb.[PostalCode]  [CustomerBillingAddressPostalCode]  ,
    sb.[Country]     [CustomerBillingAddressCountry]     
FROM
    [Customer] c
    LEFT OUTER JOIN
    [Address] sa ON c.[ShippingAddressId] = sa.[Id]
    LEFT OUTER JOIN
    [Address] sb ON c.[BillingAddressId] = sb.[Id]
WHERE
    c.[TokenDtime] >= @TokenDtime        

SELECT 
    c.[Id]           [CustomerId]                        ,
    p.[Id]           [ProductId]                         ,
    p.[Name]         [ProductName]                       ,
    p.[Price]        [ProductPrice]
FROM
    [Customer] c
    LEFT OUTER JOIN
    [CustomerProduct] cp ON c.[Id] = cp.[CustomerId]
    LEFT OUTER JOIN
    [Product] p ON cp.[ProductId] = p.[Id]
WHERE
    c.[TokenDtime] >= @TokenDtime        

0 个答案:

没有答案