Entity Framework 4.1 代码优先+MVC3 并访问某种类型的实体
我首先使用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
我的问题是:
- 如何进行查询(使用LINQ, Entity SQL或其他常用的 方式)获取所有 BorrowObjects 所在位置 BorrowedObject 是 Computer 类型?
- 如何映射查询结果 到 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:
- 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? - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用 C# 写的:
如果您不想立即加载
Computer
,则可以省略Include
。如果您项目为新类型,则不必包含
Inclusion
:User
和Computer
也将加载到BorrowComputerVM
中没有明确的包含
。is
和as
运算符确实可以在 LINQ to Entities 中工作。I write it in C#:
You can omit the
Include
if you don't want to eager load theComputer
.Includes
are not necessary if you project into a new type:User
andComputer
will be loaded in theBorrowComputerVM
also without explicitInclude
.The
is
andas
operators work indeed in LINQ to Entities.本文介绍了代码优先 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.