Populating child property with Entity Framework SqlQuery

穿精又带淫゛_ 提交于 2019-12-10 01:48:09

问题


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; }
}

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();

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();

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.


回答1:


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.




回答2:


I have used the following class structure as workaround:

public class Customer
{
    public int CustomerId { get; set; }
}

public class Order
{
    public Customer Customer { get; set; }
    private int _customerId;
    private int CustomerId { get { return _customerId; } set { Customer.CustomerId = _customerId = value; } }
    public Order()
    {
        Customer = new Customer();
    }
}

In this case you don't need to run the query twice, and the following query would give the order with customer:

db.Database.SqlQuery("select cu.CustomerId, ord.OrderId from Orders ord join Customer cu on cu.CustomerId=ord.CustomerId").ToList();




回答3:


What worked for me was to access the related member before the using is closed.

public static Customer GetCustomer (int custid)
{
Customer co = null;

using (var context = new YourEntities())
{
    // your code
    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();


    // my addition
    // cause lazy loading of Orders before closing the using
    ICollection<Order> orders = co.Orders;

    }

    // can access co.Orders after return.
    return (co);
}


来源:https://stackoverflow.com/questions/7822072/populating-child-property-with-entity-framework-sqlquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!