简体   繁体   English

如何使用实体框架获取数据库表的数量?

[英]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.

相关问题 如何使用Entity Framework 6在运行时创建数据库和表? - How can I create a database and tables at runtime using Entity Framework 6? 如何使用与Entity Framework一起使用表的存储过程 - How can I use a stored procedure that uses tables with Entity Framework 使用Entity Framework Code First开发,我能否在现有数据库中创建一个或多个表? - Using Entity Framework Code First Development, can I get it to create a table or tables in an existing database? 是否可以对未知数据库表使用实体框架? - Is it possible to use Entity Framework for unknown database tables? 如何使用 Entity Framework C# 获取与特定表相关的表名 - How I can get the name of tables has relation with specific table using Entity Framework C# 如何使用Entity Framework获取记录计数匹配谓词 - How I can get record count matching predicate using Entity Framework 如何让实体框架不为某些表创建类? - How can I make Entity Framework not create a class for some tables? 如何使用实体框架处理可选表? - How can I deal with optional tables using Entity Framework? 如何通过Entity Framework验证表之间的所有关系? - how I can verify all the releations between tables by Entity Framework? 如何使用Entity Framework Core功能更改数据库? - How can I use the Entity Framework Core capabilities to change my database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM