[英]How can I use entity framework to get a count of database tables?
I've seen posts about MetadataWorkspace but no examples for this task. 我看过有关MetadataWorkspace的帖子,但没有有关此任务的示例。 I want my code to detect if the database has had a new table added so I'm comparing the number of tables in the DB with 2 lists of known tables. 我希望我的代码能够检测数据库是否添加了新表,因此我正在将数据库中的表数与2个已知表列表进行比较。 A table can be either user data or system data and I want to delete all the data in the user tables. 一个表可以是用户数据,也可以是系统数据,我想删除用户表中的所有数据。
If a new table is discovered, the code will ask the user to define whether it contains system or user data. 如果发现了新表,该代码将要求用户定义它是否包含系统或用户数据。
This should help you . 这应该对您有帮助 。 Here is the relevant part of it for you: 这是与您相关的部分:
var metadata = ((IObjectContextAdapter)db).ObjectContext.MetadataWorkspace;
var tables = metadata.GetItemCollection(DataSpace.SSpace)
.GetItems<EntityContainer>()
.Single()
.BaseEntitySets
.OfType<EntitySet>()
.Where(s => !s.MetadataProperties.Contains("Type")
|| s.MetadataProperties["Type"].ToString() == "Tables");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.