Vb.net 如何转换EF模型';将概念实体/属性表达式中的信息存储到表/列中--即使在处理复杂属性时也是如此?
我是一个Visual Basic 2019程序,使用Entity Framework 6.4和一个模型首先创建的实体模型,编写一个针对.NET 4.6.1的应用程序,我需要通过编程将概念模型信息(如实体和属性(标量或复杂)分别转换为存储模型信息(如表和列)。(这是因为在运行时创建数据库时,我需要为实体的表创建一个二级索引,并且我不想为我的代码跟踪存储名称。) Ro Miller有一个例子,但它仅在属性为标量时有效。我的一些实体使用复杂属性,我需要以下代码(转换为VB.NET)来处理复杂属性表达式——例如,点分隔的表达式从实体上的“surface”属性一直到底层标量(即实体位置的“Address.Street.HouseNumber”) 看起来所需的更改将出现在“Return”之前的最后一行,但我不熟悉使用元数据“workspaces”,因此我需要知道需要进行哪些修改。请记住,修复程序必须执行以下操作:Vb.net 如何转换EF模型';将概念实体/属性表达式中的信息存储到表/列中--即使在处理复杂属性时也是如此?,vb.net,entity-framework,Vb.net,Entity Framework,我是一个Visual Basic 2019程序,使用Entity Framework 6.4和一个模型首先创建的实体模型,编写一个针对.NET 4.6.1的应用程序,我需要通过编程将概念模型信息(如实体和属性(标量或复杂)分别转换为存储模型信息(如表和列)。(这是因为在运行时创建数据库时,我需要为实体的表创建一个二级索引,并且我不想为我的代码跟踪存储名称。) Ro Miller有一个例子,但它仅在属性为标量时有效。我的一些实体使用复杂属性,我需要以下代码(转换为VB.NET)来处理复杂属性表达式
Option Infer On
Imports System
Imports System.Collections.Generic
Imports System.Data.Entity
Imports System.Data.Entity.Core.Mapping
Imports System.Data.Entity.Core.Metadata.Edm
Imports System.Data.Entity.Infrastructure
Imports System.Linq
Friend Class Program
Public Shared Function GetColumnName(ByVal type As Type, ByVal propertyName As String, ByVal context As DbContext) As String
Dim metadata = DirectCast(context, IObjectContextAdapter).ObjectContext.MetadataWorkspace
' Get the part of the model that contains info about the actual CLR types
Dim objectItemCollection As ObjectItemCollection = (CType(metadata.GetItemCollection(DataSpace.OSpace), ObjectItemCollection))
' Get the entity type from the model that maps to the CLR type
Dim entityType = metadata.GetItems(Of EntityType)(DataSpace.OSpace).Single(Function(e) objectItemCollection.GetClrType(e) Is type)
' Get the entity set that uses this entity type
Dim entitySet = metadata.GetItems(Of EntityContainer)(DataSpace.CSpace).Single().EntitySets.Single(Function(s) s.ElementType.Name = entityType.Name)
' Find the mapping between conceptual and storage model for this entity set
Dim mapping = metadata.GetItems(Of EntityContainerMapping)(DataSpace.CSSpace).Single().EntitySetMappings.Single(Function(s) s.EntitySet.Equals(entitySet))
' Find the storage entity set (table) that the entity is mapped
Dim tableEntitySet = mapping.EntityTypeMappings.Single().Fragments.Single().StoreEntitySet
' Return the table name from the storage entity set
Dim tableName = If(tableEntitySet.MetadataProperties("Table").Value, tableEntitySet.Name)
' Find the storage property (column) that the property is mapped
Dim columnName = _
mapping.EntityTypeMappings.Single().Fragments.Single().PropertyMappings.OfType(Of ScalarPropertyMapping)().Single(Function(m) m.Property.Name = propertyName).Column.Name
Return tableName.ToString & "." & columnName
End Function
End Class