大佬教程收集整理的这篇文章主要介绍了c# – 首先从实体框架6数据库中的数据库中获取列名,并在映射中使用不同的属性名称,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public class AGENTMap : EntityTypeConfiguration<AGENT> { public AGENTMap() { // PriMary Key this.HasKey(t => new {t.AgentCode }); // Properties this.Property(t => t.AgentCodE) .HasmaxLength(10); this.Property(t => t.AgentName) .HasmaxLength(30); // Table & column Mappings this.@R_757_10586@ble("AGENT"); this.Property(t => t.agent_cd).HascolumnName("agent_cd"); this.Property(t => t.agent_nm).HascolumnName("agent_nm"); } }
这相当于具有这些属性的AGENT类.
问题是当我尝试使用此代码段获取主键时:
ObjectContext objectContext = ((IObjectContextAdapter)_context).objectContext; ObjectSet<TEntity> objSet = objectContext.CreateObjectSet<TEntity>(); IEnumerable<String> keyNames = objSet.EntitySet.ElementType.Keymembers .Where(p => p.MetadataProperties.Any(m => m.PropertyKind == PropertyKind.Extended && Convert.ToString(m.value) == "Identity")) .SELEct(e => e.Name).ToList(); return keyNames;
它返回Mapping类的Property Name.我想得到“agent_cd”..这是db列名.在EF6中是否有办法在Db上获取确切的列名?
public static String GetcolumnName(Type type,String propertyName,DbContext context) { var Metadata = ((IObjectContextAdapter)context).objectContext.MetadataWorkspace; // Get the part of the model that contains info about the actual CLR types var objectItemCollection = ((ObjectItemCollection)Metadata.GetItemCollection(DataSpace.oSpacE)); // Get the entity type from the model that maps to the CLR type var entityType = Metadata .GetItems<EntityType>(DataSpace.oSpacE) .Single(e => objectItemCollection.GetClrType(E) == typE); // Get the entity set that uses this entity type var entitySet = Metadata .GetItems<EntityContainer>(DataSpace.CSpacE) .Single() .EntitySets .Single(s => s.ElementType.Name == entityType.Name); // Find the mapping between conceptual and storage model for this entity set var mapping = Metadata.GetItems<EntityContainerMapping>(DataSpace.CSSpacE) .Single() .EntitySetMappings .Single(s => s.EntitySet == entitySet); // Find the storage entity set (tablE) that the entity is mapped var tableEntitySet = mapping .EntityTypeMappings.Single() .Fragments.Single() .StoreEntitySet; // Return the table name from the storage entity set var tablename = tableEntitySet.MetadataProperties["Table"].Value ?? tableEntitySet.Name; // Find the storage property (column) that the property is mapped var columnName = mapping .EntityTypeMappings.Single() .Fragments.Single() .Propertymappings .ofType<ScalarPropertymapping>() .Single(m => m.Property.Name == propertyName) .column .Name; return tablename + "." + columnName; }
以上是大佬教程为你收集整理的c# – 首先从实体框架6数据库中的数据库中获取列名,并在映射中使用不同的属性名称全部内容,希望文章能够帮你解决c# – 首先从实体框架6数据库中的数据库中获取列名,并在映射中使用不同的属性名称所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。