当我调用这个linq查询时,我得到以下错误:
实体或复杂类型"数据模型"。
public static IEnumerable<CustomerContacts> GetList(int customerId = null)
{
using (var context = new AppContext())
{
var cList = (from cc in context.CustomerContacts
join c in context.Customers on cc.CustomerId equals c.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
}).ToList();
return objList;
}
}
如果我选择cc,那么cc. customername将不会被设置
你可以这样做:
from cc in context.CustomerContacts.Include(cc => cc.CustomerName)
…来为您自动加载导航属性。搜索"EF导航属性延迟加载"
注意,你需要有'using…一些命名空间,我不记得了,让语法工作,因为它使用扩展方法。否则,只需引用想要加载的导航属性的名称,如:
from cc in context.CustomerContacts.Include("CustomerName")