DbSet表名

要在Entity framework 4.0上获取数据库表名,我会这样做:

ObjectSetInstance.EntitySet.ToString() 

有没有办法在Entity Framework 4.1上执行此操作?

尝试这样的事情:

 string name = (context as IObjectContextAdapter).ObjectContext.CreateObjectSet().EntitySet.Name; 

DbContextObjectContext的扩展方法:

 public static class ContextExtensions { public static string GetTableName(this DbContext context) where T : class { ObjectContext objectContext = ((IObjectContextAdapter) context).ObjectContext; return objectContext.GetTableName(); } public static string GetTableName(this ObjectContext context) where T : class { string sql = context.CreateObjectSet().ToTraceString(); Regex regex = new Regex("FROM (?.*) AS"); Match match = regex.Match(sql); string table = match.Groups["table"].Value; return table; } }

使用ObjectContext对象:

 ObjectContext context = ....; string table = context.GetTableName(); 

使用DbContext对象:

 DbContext context = ....; string table = context.GetTableName(); 

更多信息:

entity framework:从实体获取映射表名称

从EF6.1开始,本文探讨的解决方案展示了如何使用新公开的元数据来完成此任务,不应要求初始化数据库或依赖于使用何种方法设置表名。

你可以尝试这样的事情。

 private string GetTableName(Type type) { var tableAttribute = type.GetCustomAttributes(false).OfType().FirstOrDefault(); return tableAttribute == null ? type.Name : tableAttribute.Name; } 

你可以像这样调用这个字符串。

 var tableName = GetTableName(entityType.FirstOrDefault()); 

有关详细信息,请参阅以下链接。 链接

我还发现CreateObjectSet()并不能完成这项工作,特别是在获取用于SqlBulCopy()的列名时。 以下用于获取EntitySet的代码似乎更好,

  private EntitySet GetEntitySet(DbContext context) { var type = typeof(T); var entityName = type.Name; var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace; IEnumerable entitySets; entitySets = metadata.GetItemCollection(DataSpace.SSpace) .GetItems() .Single() .BaseEntitySets .OfType() .Where(s => !s.MetadataProperties.Contains("Type") || s.MetadataProperties["Type"].ToString() == "Tables"); var entitySet = entitySets.FirstOrDefault(t => t.Name == entityName); return entitySet; } 

为了获取表名,我使用了另外两种方法而不是EntitySet.Name ,它返回单数而不是复数的名称。

 public string GetTableName(DbContext context) where T: class { var entitySet= GetEntitySet(context); if (entitySet == null) throw new Exception("Unable to find entity set '{0}' in edm metadata".F(typeof(T).Name)); var tableName = GetStringProperty(entitySet, "Schema") + "." + GetStringProperty(entitySet, "Table"); return tableName; } private string GetStringProperty(MetadataItem entitySet, string propertyName) { MetadataProperty property; if (entitySet == null) throw new ArgumentNullException("entitySet"); if (entitySet.MetadataProperties.TryGetValue(propertyName, false, out property)) { string str = null; if (((property != null) && (property.Value != null)) && (((str = property.Value as string) != null) && !string.IsNullOrEmpty(str))) { return str; } } return string.Empty; } 

这将返回数据库中使用的实际名称以及db模式(如果您不使用dbo),并且应该使用数据注释或流畅配置。

唯一需要注意的是数据库应该已经初始化了。 (我认为使用CreateObjectSet()的问题 – 缺少一个解决db模式的步骤)。

资料来源: https : //msdn.microsoft.com/en-gb/data/jj819164.aspx

 using System.Data.Entity; using System.Data.Entity.Infrastructure.DependencyResolution; using System.Data.Entity.Infrastructure.Pluralization; var service = DbConfiguration.DependencyResolver.GetService(); var entitySetName = service.Pluralize(typeof(TEntity).Name));