c# – 如何以编程方式从实体获取具有模式名称的数据库表名

参见英文答案 Get Database Table Name from Entity Framework MetaData 22个我正在尝试获取实体的SQL表名称.当我使用MetadataWorkspace查询时,我从对象或存储空间获取大量信息...

参见英文答案 > Get Database Table Name from Entity Framework MetaData                                    22个
我正在尝试获取实体的SQL表名称.
当我使用MetadataWorkspace查询时,我从对象或存储空间获取大量信息.但架构名称和表名称不存在.

我尝试查询CSpace和SSpace的所有EntityType对象,我可以看到两者都正确列出,但我无法弄清楚如何从CSpace获取SSpace.

所以说我在对象模型中有一个名为User的类型 – 如何在数据库中找到带有模式名称(tkp.User)的表名?

有没有办法做到这一点?

解决方法:

对此没有完美的答案,但这应该有效……

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

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

You’d need to use reflection in the last part.
Good news is it should transparently work with EF6 too
(as it has a similar base class)

(它与模式类似,也应该在那里 – 这是Db / SQL映射名称,facet等的空间)

看到我的这篇文章有很多细节
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
Get Database Table Name from Entity Framework MetaData
http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

本文标题为:c# – 如何以编程方式从实体获取具有模式名称的数据库表名

基础教程推荐