Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(二)
2018-09-26 12:59 音乐让我说 阅读(191) 评论(0) 编辑 收藏 举报接着上一篇
直接贴代码了:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Core.Mapping; using System.Data.Entity.Core.Metadata.Edm; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration; using System.Data.Entity.ModelConfiguration.Conventions; using System.Linq; using EntityFramework.Extensions; namespace EntityFrameworkSample { class Program { static void Main(string[] args) { using (var db = new BloggingContext(@"Data Source=.\SQLExpress;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;Password=123456")) { string blogTableName = GetTableName(typeof(Blog), db); string postTableName = GetTableName(typeof(Post), db); //Console.WriteLine("Blog maps to: {0}", blogTableName); //Console.WriteLine("Post maps to: {0}", postTableName); string blogUrlColumnName = GetColumnName(typeof(Blog), "BlogUrl", db); string postTitleColumnName = GetColumnName(typeof(Post), "PostTitle", db); Console.WriteLine("Blog.BlogUrl maps to: {0}.{1}", blogTableName, blogUrlColumnName); Console.WriteLine("Post.PostTitle maps to: {0}.{1}", postTableName, postTitleColumnName); } Console.ReadLine(); } public static string GetTableName(Type type, 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 table = mapping .EntityTypeMappings.Single() .Fragments.Single() .StoreEntitySet; // Return the table name from the storage entity set return (string)table.MetadataProperties["Table"].Value ?? table.Name; } 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; return columnName; } } public class BloggingContext : DbContext { public BloggingContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new BlogMap()); modelBuilder.Configurations.Add(new PostMap()); modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); base.OnModelCreating(modelBuilder); } } public class Blog { public int BlogId { get; set; } public string BlogUrl { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string PostTitle { get; set; } public string Body { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } public class BlogMap : EntityTypeConfiguration<Blog> { public BlogMap() { this.HasKey(c => c.BlogId); this.ToTable("t_blog"); this.Property(c => c.BlogUrl).HasColumnName("Url"); } } public class PostMap : EntityTypeConfiguration<Post> { public PostMap() { this.HasKey(c => c.PostId); this.ToTable("t_post"); this.Property(c => c.PostTitle).HasColumnName("Title"); } } }
运行截图:
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步