实体框架 4 仅代码从 POCO 域对象的元数据获取表名称

发布于 2024-09-25 05:59:27 字数 158 浏览 2 评论 0原文

您好,我仅使用 CTP4 中的实体框架代码。我的问题是:给定使用 EntityConfiguration 映射的域类的名称,如何在运行时检索映射类的表名称?我假设我需要在 ObjectContext 上使用 MetadataWorkspace,但发现很难获取最新的文档。任何帮助或链接将不胜感激。谢谢。

Hi I am using Entity Framework code only from CTP4. My question is this: given the name of a domain class which is mapped using an EntityConfiguration how can I retrieve the table name for the mapped class at runtime? I'm assuming I need to be using the MetadataWorkspace on the ObjectContext, but finding it difficult to get up-to-date documentation. Any help or links would be much appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2024-10-02 05:59:27

是的,您是对的,所有映射信息都可以通过 MetadataWorkspace 检索。

下面是退休 Product 类的表和架构名称的代码:

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; }
}

现在,要检索 Product 类的表名称,您必须创建 DbContext 并使用以下代码:

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").
}

我怀疑这是一个完美的解决方案,但正如我之前使用的那样,它的工作没有任何问题。

Yes you are right, all mapping information could be retrieved through MetadataWorkspace.

Below is the code to retireve table and schema names for Product class:

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; }
}

Now, to retrieve table name for Product class you have to create DbContext and use following code:

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").
}

I doubt that this is a perfect solution but as I used it before and it worked without issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文