Entity Framework 4.1 代码优先+MVC3 并访问某种类型的实体

发布于 2024-11-07 03:35:14 字数 1337 浏览 0 评论 0原文

我首先使用Entity Framework 4.1代码+MVC3,我使用的继承策略是TPC

我有以下类

Public Class ObjectBase
    <Key()>
    Public Property Id As Integer

    Public Property Description As String
End Class

Public Class Computer
    Inherits ObjectBase

    Public Property Computername As String
End Class

Public Class Book
    Inherits ObjectBase

    Public Property BookName As String
End Class

Public Class User
    <Key()>
    Public Property Id As Integer

    Public Property Name As String
End Class

Public Class BorrowObject
    <Key()>
    Public Property Id As Integer

    Public Property User As User

    Public Property BorrowedObject as ObjectBase
End Class

Public Class BorrowComputerVM
    <Key()>
    Public Property Id As Integer

    Public Property User As User

    Public Property Computer as Computer
End Class

我的问题是:

  1. 如何进行查询(使用LINQ, Entity SQL或其他常用的 方式)获取所有 BorrowObjects 所在位置 BorrowedObject 是 Computer 类型?
  2. 如何映射查询结果 到 ViewModel 称为 “BorrowComputerVM”(用于 创建视图仅用于 借用电脑)。

问题1(和问题2)应该非常简单,但我已经在谷歌上花了几个小时才找到答案,但根本没有结果。我发现的唯一一件事是,您可以通过编写 context.ObjectBase.OfType(Of Computer) 来获取 ObjectBase 中的所有计算机,但这没有帮助,因为您无法编写 context.BorrowObjects.ObjectBase.OfType(Of Computer)

请提供代码VB.NET 中的示例(如果可以的话),但更重要的是:请确保您提供的代码示例无需经过数小时的修改即可工作!

I am using Entity Framework 4.1 code first+MVC3 and the inheritence stratagy that I use is TPC

I have the following classes

Public Class ObjectBase
    <Key()>
    Public Property Id As Integer

    Public Property Description As String
End Class

Public Class Computer
    Inherits ObjectBase

    Public Property Computername As String
End Class

Public Class Book
    Inherits ObjectBase

    Public Property BookName As String
End Class

Public Class User
    <Key()>
    Public Property Id As Integer

    Public Property Name As String
End Class

Public Class BorrowObject
    <Key()>
    Public Property Id As Integer

    Public Property User As User

    Public Property BorrowedObject as ObjectBase
End Class

Public Class BorrowComputerVM
    <Key()>
    Public Property Id As Integer

    Public Property User As User

    Public Property Computer as Computer
End Class

My questions are:

  1. How do I do a query (using LINQ,
    Entity SQL or other commonly used
    way) to get all BorrowObjects where
    BorrowedObject is of type Computer?
  2. How do I map the result of the query
    to the ViewModel called
    "BorrowComputerVM" (used for
    creating views only used for
    borrowing a Computer).

Question 1 (and question 2) should be very simple, but I have allready spent hours on Google to find an answer with no result at all. The only thing I have found is that you can get all computers in ObjectBase by writing context.ObjectBase.OfType(Of Computer), and that does not help since you cannot write context.BorrowObjects.ObjectBase.OfType(Of Computer)

Please provide code samples in VB.NET (if you can), but more importantly: Please ensure that the codesamples you supply work without hours of modification!

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

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

发布评论

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

评论(2

染墨丶若流云 2024-11-14 03:35:14

我用 C# 写的:

如何进行查询(使用 LINQ、Entity SQL 或其他常用方式)
获取所有 BorrowObjects 所在位置
BorrowedObject 的类型
电脑?

var list = context.BorrowObjects.Include(b => b.BorrowedObject)
                  .Where(b => b.BorrowedObject is Computer)
                  .ToList();

如果您不想立即加载Computer,则可以省略Include

如何将查询结果映射到名为的 ViewModel
“BorrowComputerVM”(用于创建
视图仅用于借用
计算机)。

var list = context.BorrowObjects
                  .Where(b => b.BorrowedObject is Computer)
                  .Select(b => new BorrowComputerVM
                          {
                              Id = b.Id,
                              User = b.User,
                              Computer = b.BorrowedObject as Computer
                          })
                  .ToList();

如果您项目为新类型,则不必包含 InclusionUserComputer 也将加载到 BorrowComputerVM 中没有明确的包含

isas 运算符确实可以在 LINQ to Entities 中工作。

I write it in C#:

How do I do a query (using LINQ, Entity SQL or other commonly used way)
to get all BorrowObjects where
BorrowedObject is of type
Computer?

var list = context.BorrowObjects.Include(b => b.BorrowedObject)
                  .Where(b => b.BorrowedObject is Computer)
                  .ToList();

You can omit the Include if you don't want to eager load the Computer.

How do I map the result of the query to the ViewModel called
"BorrowComputerVM" (used for creating
views only used for borrowing a
Computer).

var list = context.BorrowObjects
                  .Where(b => b.BorrowedObject is Computer)
                  .Select(b => new BorrowComputerVM
                          {
                              Id = b.Id,
                              User = b.User,
                              Computer = b.BorrowedObject as Computer
                          })
                  .ToList();

Includes are not necessary if you project into a new type: User and Computer will be loaded in the BorrowComputerVM also without explicit Include.

The is and as operators work indeed in LINQ to Entities.

可爱暴击 2024-11-14 03:35:14

本文介绍了代码优先 TPC 继承所需的大量配置,以及您需要使用的一些特殊技术需要使用。所有这些都已经完成了吗?如果您已完成本文中描述的所有编码柔术,那么查询语法或对象结构似乎不会对您构成太大障碍。

This article describes a lot of configuration that's required for code-first TPC inheritance, as well as some special techniques you need to use. Has all of that already been done? If you've completed all the coding jujitsu described in the article, it seems like the query syntax or object structure shouldn't pose much of an obstacle to you.

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