IQueryable 实体框架 POCO 映射

发布于 2024-10-01 05:50:38 字数 1084 浏览 6 评论 0原文

我正在使用 ASP.NET MVC2 和 EF4。我需要为我的两个类 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
 //}
});
  1. 如果没有 .Include("Address") 我不会从地址表中检索任何内容,这是正确的吗?

  2. 如何使用上面的 Select() 语句将 Address 映射到 PersonP 内的 AddressP?< /p>

谢谢。

I am using ASP.NET MVC2 with EF4. I need to create POCOs for two of my classes PersonP and AddressP, which correspond to their EF4 'complex' classes (which include things like navigation properties and OnPropertyChanged()). Mapping just PersonP by itself works fine, but PersonP contains AddressP (foreign key) - how do I map this using an IQueryable expression?

Here is what I've tried:

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
 //}
});
  1. Without the .Include("Address") I would not retrieve anything from the Address table is this correct?

  2. How do I map Address to AddressP inside PersonP, using the Select() statement above?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2024-10-08 05:50:38
  1. 这是正确的,如果您已禁用延迟加载或您的对象上下文已被释放并且无法用于延迟加载工作。
  2. 是的,它不会工作,因为首先您需要执行查询,然后开始映射它,否则您的映射逻辑将被认为在数据库中运行,因此会出现异常。
    这样的事情会起作用:
// First we execute the query:
IQueryable<PersonP> persons = _repo.QueryAll().Include("Address").ToList();

// Now we have a IEnumerable and we can safely do the mappings:
persons.Select(p => new PersonP
{
    Id = p.Id,
    FirstName = p.FirstName,
    Address = (p.Address == null) ? null : new AddressP()
    {
        Id = p.Address.Id,
        Street = p.Address.Street
    }
}).ToList();

虽然这个解决方案可以解决问题,但如果目的是拥有 POCO 类,您绝对应该考虑利用 EF4.0 POCO 支持并直接在 EF 中使用 POCO 类,而不是事后映射它们。一个好的起点是这个演练:

演练:实体框架的 POCO 模板

  1. That is correct, if you've disable Lazy Loading or your object context has been disposed already and cannot be used to get Lazy Loading working.
  2. Yes, it won't work since first you need to execute your query and then start mapping it, otherwise your mapping logic would be taken to be run in the database hence the exception.
    Something like this will work:
// First we execute the query:
IQueryable<PersonP> persons = _repo.QueryAll().Include("Address").ToList();

// Now we have a IEnumerable and we can safely do the mappings:
persons.Select(p => new PersonP
{
    Id = p.Id,
    FirstName = p.FirstName,
    Address = (p.Address == null) ? null : new AddressP()
    {
        Id = p.Address.Id,
        Street = p.Address.Street
    }
}).ToList();

While this solution will do the trick but if the intention is to have POCO classes you should definitely consider to take advantage of EF4.0 POCO support and use POCO classes directly with EF instead of mapping them afterward. A good place to start would be this walkthrough:

Walkthrough: POCO Template for the Entity Framework

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文