将实体框架代码属性映射到数据库列(C空间到空间)

时间:2014-04-25 15:49:07

标签: .net vb.net entity-framework

我正在尝试实现一个属性,我可以在我的代码第一个Entity Framework数据模型中应用属性,以指示在创建数据库时将应用的唯一约束。我有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元数据框架的速度很慢。我希望那些更熟悉它的人可以加快速度。

1 个答案:

答案 0 :(得分:3)

我不确定它是完整还是可靠,但我终于找到了适用于我的第一个测试用例的代码:

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 entityMapping = ws.GetItems(Of EntityContainerMapping)(DataSpace.CSSpace).Single.EntitySetMappings
Dim associations = ws.GetItems(Of EntityContainerMapping)(DataSpace.CSSpace).Single.AssociationSetMappings
For Each setType In entityTypes
   Dim cSpaceEntitySet = entityContainer.EntitySets.SingleOrDefault( _
      Function(t) t.ElementType.Name = setType.Name)
   If cSpaceEntitySet Is Nothing Then Continue For ' Derived entities will be skipped
   Dim sSpaceEntitySet = entityMapping.Single(Function(t) t.EntitySet Is cSpaceEntitySet)
   Dim tableInfo As MappingFragment
   If sSpaceEntitySet.EntityTypeMappings.Count = 1 Then
      tableInfo = sSpaceEntitySet.EntityTypeMappings.Single.Fragments.Single
   Else
      ' Select only the mapping (esp. PropertyMappings) for the base class
      tableInfo = sSpaceEntitySet.EntityTypeMappings.Where(Function(m) m.IsOfEntityTypes.Count _
         = 1 AndAlso m.IsOfEntityTypes.Single.Name Is setType.Name).Single().Fragments.Single
   End If
   Dim tableName = If(tableInfo.StoreEntitySet.Table, tableInfo.StoreEntitySet.Name)
   Dim schema = tableInfo.StoreEntitySet.Schema
   Dim clrType = Type.GetType(setType.FullName)
   Dim uniqueCols As IList(Of String) = Nothing
   For Each propMap In tableInfo.PropertyMappings.OfType(Of ScalarPropertyMapping)()
      Dim clrProp = clrType.GetProperty(propMap.Property.Name)
      If Attribute.GetCustomAttribute(clrProp, GetType(UniqueAttribute)) IsNot Nothing Then
         If uniqueCols Is Nothing Then uniqueCols = New List(Of String)
         uniqueCols.Add(propMap.Column.Name)
      End If
   Next
   For Each navProp In setType.NavigationProperties
      Dim clrProp = clrType.GetProperty(navProp.Name)
      If Attribute.GetCustomAttribute(clrProp, GetType(UniqueAttribute)) IsNot Nothing Then
         Dim assocMap = associations.SingleOrDefault(Function(a) _
            a.AssociationSet.ElementType.FullName = navProp.RelationshipType.FullName)
         Dim sProp = assocMap.Conditions.Single
         If uniqueCols Is Nothing Then uniqueCols = New List(Of String)
         uniqueCols.Add(sProp.Column.Name)
      End If
   Next
   If uniqueCols IsNot Nothing Then
      Dim propList = uniqueCols.ToArray()
      context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX IX_" & tableName & "_" & String.Join("_", propList) _
         & " ON " & schema & "." & tableName & "(" & String.Join(",", propList) & ")")
   End If
Next