【问题标题】:How get database table name with schema name from entity programmatically [duplicate]如何以编程方式从实体获取具有模式名称的数据库表名[重复]
【发布时间】:2013-04-11 10:58:23
【问题描述】:

我正在尝试获取实体的 SQL 表名。 当我使用 MetadataWorkspace 查询时,我会从对象或存储空间中获得大量信息。但是模式名和表名不存在。

我尝试查询 CSpace 和 SSpace 的所有 EntityType 对象,我可以看到两者都正确列出,但我不知道如何从 CSpace 获取 SSpace。

假设我在对象模型中有一个名为 User 的类型 - 如何在数据库中找到具有模式名称 (tkp.User) 的表名?

有什么办法吗?

【问题讨论】:

    标签: c# linq entity-framework


    【解决方案1】:

    没有perfect 对此的回答,但这应该可以工作......

    var ssSpaceSet = objectContext.MetadataWorkspace
        .GetItems<EntityContainer>(DataSpace.SSpace).First()
        .BaseEntitySets
        .First(meta => meta.ElementType.Name == "YourEntityName");
    

    如果您在调试器中查找,Table 属性应该具有真实的表名。

    您需要在最后一部分使用reflection
    好消息是它也应该透明地与 EF6 一起使用 (因为它有类似的基类)

    schema 与此类似,也应该在其中 - 这是用于 Db/SQL 映射名称、构面等的 space

    查看我的这篇文章,了解很多细节
    Get Model schema to programmatically create database using a provider that doesn't support CreateDatabase


    Get Model schema to programmatically create database using a provider that doesn't support CreateDatabase
    How I can read EF DbContext metadata programmatically?
    How check by unit test that properties mark as computed in ORM model?
    Programmatic data transformation in EF5 Code First migration
    Entity Framework MigrationSqlGenerator for SQLite
    http://entityframework.codeplex.com/
    Entity Framework - Get Table name from the Entity
    ef code first: get entity table name without dataannotations
    @987654330 @
    http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

    【讨论】:

    • 非常感谢。我通过 Schema 属性找到了模式名称。
    • 如何从简单的 DbContext 中获取模式名称?
    • @evc 你有什么问题?这是一个粗略的解决方案和表格,架构都在里面,您可以直接从 DbContext 获取 ObjectContext,PaulP 提到了架构属性等。在 EF 6.*(新的)中,您有更好的方法 stackoverflow.com/a/18964974/417747 或 @ 987654333@
    • 哦,发现很简单。 (ObjectContext)MyDbContext
    猜你喜欢
    • 2018-05-07
    • 1970-01-01
    • 2019-03-07
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多