在实体框架6数据库中,首先使用存储过程时,我只需执行所有左外连接和内连接,并在我的查询中选择任何特定的导航属性,其名称与我的扩展属性名称相匹配。然后在我的数据库中,我使用存储过程设置的任何导航属性扩展自动生成的类,这样我就可以执行以下操作:(在此示例中,我只需设置CustomerName属性,这是我手动添加到CustomerContact的属性我的扩展课程 (即)
public partial class CustomerContact {
public string CustomerName { get; set; }
}
public static IEnumerable<CustomerContact> GetList(int customerId = null)
{
using (var context = new LowthersContext())
{
var procList = context.GetCustomerContactsProc(customerId);
var ccList = (from cc in procList
select new Material()
{
Id = cc.Id,
FirstName = cc.FirstName,
LastName = cc.LastName,
Email = cc.Email
// navigation properties from joins
CustomerName = cc.CustomerName // this property is added in partial class
}).ToList();
return ccList;
}
}
以上工作正常,它有点使用存储过程的oldschool技术,但我想将我的一些简单存储过程转换为使用Linq到SQL。这是我的尝试,我收到了一个错误:
实体或复杂类型&#39; Data Model.CustomerContact&#39;无法在LINQ to Entities查询中构造。
var cList = (from cc in context.CustomerContacts
join c in context.Customers on cc.CustomerId equals c.Id
join cl in context.CustomerLocations on c.LocationId = cl.Id
where (customerId == null || cc.CustomerId == customerId)
select new CustomerContact
{
Id = cc.Id,
FirstName = cc.FirstName,
LastName = cc.LastName,
Email = cc.Email,
// navigation properties
CustomerName = c.Name
ContactLocationName = cl.LocationName
}).ToList();
return cList;
我真的想忽略Entity Framework自动为我生成的导航属性,并根据需要设置单个属性,而不是整个对象以提高性能。