Orm 实体框架4代码仅从POCO域对象的元数据中获取表名
嗨,我只使用CTP4中的实体框架代码。我的问题是:给定使用EntityConfiguration映射的域类的名称,如何在运行时检索映射类的表名?我假设我需要在ObjectContext上使用MetadataWorkspace,但发现很难获取最新文档。任何帮助或链接将不胜感激。谢谢。是的,您是对的,所有映射信息都可以通过MetadataWorkspace检索 下面是Orm 实体框架4代码仅从POCO域对象的元数据中获取表名,orm,entity-framework-4,code-first,ef4-code-only,Orm,Entity Framework 4,Code First,Ef4 Code Only,嗨,我只使用CTP4中的实体框架代码。我的问题是:给定使用EntityConfiguration映射的域类的名称,如何在运行时检索映射类的表名?我假设我需要在ObjectContext上使用MetadataWorkspace,但发现很难获取最新文档。任何帮助或链接将不胜感激。谢谢。是的,您是对的,所有映射信息都可以通过MetadataWorkspace检索 下面是产品类的停用表和架构名称的代码: public class Product { public Guid Id { get; s
产品
类的停用表和架构名称的代码:
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
HasKey(e => e.Id);
Property(e => e.Id)
.HasColumnName(typeof(Product).Name + "Id");
Map(m =>
{
m.MapInheritedProperties();
m.ToTable("ProductsTable");
});
Property(p => p.Name)
.IsRequired()
.IsUnicode();
}
}
public class Database : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ProductConfiguration());
}
public DbSet<Product> Products { get; set; }
}
我怀疑这是否是一个完美的解决方案,但正如我以前使用的那样,它没有任何问题
using(var dbContext = new Database())
{
var adapter = ((IObjectContextAdapter)dbContext).ObjectContext;
StoreItemCollection storageModel = (StoreItemCollection)adapter.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
var containers = storageModel.GetItems<EntityContainer>();
EntitySetBase productEntitySetBase = containers.SelectMany(c => c.BaseEntitySets.Where(bes => bes.Name == typeof(Product).Name)).First();
// Here are variables that will hold table and schema name
string tableName = productEntitySetBase.MetadataProperties.First(p => p.Name == "Table").Value.ToString();
string schemaName = productEntitySetBase.MetadataProperties.First(p => p.Name == "Schema").
}