提问人:Andy 提问时间:9/22/2023 最后编辑:Svyatoslav DanylivAndy 更新时间:9/22/2023 访问量:28
如何使用批注获取 EF Core 7 中的特定列
How to get specific columns in EF Core 7 with ANNOTATIONS
问:
我正在尝试学习如何使用 EF Core。 我正在使用 VB、EF Core 7、MySQL DB,我正在尝试做一个本地化的应用程序,其中表的列是本地化的。 我搭建了数据库的脚手架,我有这些实体:
Partial Public Class Board
<Key>
<Column("BoardID")>
Public Property BoardId As Integer
<StringLength(100)>
<CustomDisplayName("BoardName")>
Public Property BoardName As String
<Column(TypeName:="text")>
Public Property Description As String
End Class
Partial Public Class Item
<Key>
<Column("ItemID")>
Public Property ItemId As Integer
<Column("BoardID")>
Public Property BoardId As Integer?
Public Property CreationDate As DateOnly?
Public Property LastModifiedDate As DateOnly?
<StringLength(100)>
Public Property Title As String
End Class
Partial Public Class Subitem
<Key>
<Column("SubitemID")>
Public Property SubitemId As Integer
<Column("ItemID")>
Public Property ItemId As Integer?
<Column("CreatorUserID")>
Public Property CreatorUserId As Integer?
Public Property CreationDate As DateOnly?
Public Property LastModifiedDate As DateOnly?
<Column(TypeName:="text")>
Public Property Description As String
如果我这样做:
Using Context As New Context
Dim result = Context.Boards.ToList
GridControl1.datasource = result
End Using
本地化标题正在工作,但向我显示了所有 3 个表中的所有列,例如内部连接它们,我只想从 boards 表中显示。
如果我这样做:
Using Context As New Context
Dim result = Context.Boards.[Select](Function(Boards) New With {Boards.BoardName, Boards.Description}).ToList
GridControl1.datasource = result
End Using
准确显示我想要的列,但由于匿名类型,注释将不起作用。
我也试过:
Using Context As New Context
Dim data = Context.Boards.AsQueryable()
data = From recordset In Context.Boards Select New Board With {
.BoardName = recordset.BoardName,
.Description = recordset.Description
}
Dim Result As List(Of Board) = data.ToList()
GridControl1.DataSource = Result
End Using
但不是只显示 2 列,而是显示内部连接的所有列。本地化字幕正在工作。
任何建议:
- 如何显示具有自定义数据注释工作的两列?
- 如何只显示板表而不是 3 个表的内部连接?
答: 暂无答案
评论