【发布时间】:2014-06-11 10:28:45
【问题描述】:
我正在尝试实现一个属性,该属性可以应用于我的代码优先实体框架数据模型中的属性,以指示创建数据库时将应用的唯一约束。我有read about extracting EF table mapping information 使用为EF 6.1 公开的映射API,我有read about implementing a custom attribute to indicate which properties represent a natural key。但是,除非我误读了代码,否则我认为这仅在 CLR 属性名称(OSpace / CSpace)与 SQL 列名称(SSpace)匹配时才有效。我希望能够在我的唯一键中包含关联属性(外键),例如此示例中的 EnumList:
Public Class EnumValue
Public Property EnumValueId As Integer
Public Property Sequence As Integer
Public Overridable Property Label As TranslatedString
<MaxLength(5), MyUnique()> _
Public Property Value As String
<MyUnique()> _
Public Overridable Property EnumList As EnumList
End Class
我从这么多代码开始,它基于有关映射表名称的链接文章
Dim ws = DirectCast(context, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext.MetadataWorkspace
Dim oSpace = ws.GetItemCollection(Core.Metadata.Edm.DataSpace.OSpace)
Dim entityTypes = oSpace.GetItems(Of EntityType)()
Dim entityContainer = ws.GetItems(Of EntityContainer)(DataSpace.CSpace).Single()
Dim mapping = ws.GetItems(Of EntityContainerMapping)(DataSpace.CSSpace).Single.EntitySetMappings
For Each setType In entityTypes
Dim cSpaceEntitySet = entityContainer.EntitySets.Single(Function(t) t.ElementType.Name = setType.Name)
Dim sSpaceEntitySet = mapping.Single(Function(t) t.EntitySet Is cSpaceEntitySet)
Dim tableInfo = sSpaceEntitySet.EntityTypeMappings.Single.Fragments.Single
Dim tableName = If(tableInfo.StoreEntitySet.Table, tableInfo.StoreEntitySet.Name)
Dim schema = tableInfo.StoreEntitySet.Schema
这足以获得我需要的有关表名的信息,但现在我需要以某种方式将 CLR 属性名链接到 SQL 列名,而且理解 EF 元数据框架的速度很慢。我希望更熟悉它的人可以加快速度。
【问题讨论】:
标签: .net vb.net entity-framework