【发布时间】:2011-05-06 11:05:17
【问题描述】:
我正在使用带有 EF4 的 ASP.NET MVC2。我需要为我的两个类 PersonP 和 AddressP 创建 POCO,它们对应于它们的 EF4“复杂”类(包括导航属性和 OnPropertyChanged() 等内容)。仅映射 PersonP 本身可以正常工作,但 PersonP 包含 AddressP(外键) - 如何使用 IQueryable 表达式映射它?
这是我尝试过的:
class AddressP
{
int Id { get; set; }
string Street { get; set; }
}
class PersonP
{
int Id { get; set; }
string FirstName { get; set; }
AddressP Address { get; set; }
}
IQueryable<PersonP> persons = _repo.QueryAll()
.Include("Address")
.Select(p => new PersonP
{
Id = p.Id,
FirstName = p.FirstName,
//Address = p.Address <-- I'd like to do this, but p.Address is Address, not AddressP
//Address = (p.Address == null) ? null :
//new AddressP <-- does not work; can't use CLR object in LINQ runtime expression
//{
// Id = p.Address.Id,
// Street = p.Address.Street
//}
});
如果没有
.Include("Address"),我将无法从地址表中检索任何内容,这是否正确?如何使用上面的
Select()语句将Address映射到PersonP内的AddressP?
谢谢。
【问题讨论】:
标签: entity-framework entity-framework-4 poco iqueryable