简体   繁体   English

Entity Framework Core:获取所有表的列表

[英]Entity Framework Core: Get a List of All the Tables

How do I get a list of all the tables in Entity Framework Core in a db context?如何在 db 上下文中获取 Entity Framework Core 中所有表的列表?

The following answer is for previous version in Entity Framework 5, we are currently using EF Core 3.以下答案适用于 Entity Framework 5 中的先前版本,我们目前使用的是 EF Core 3。

how to get a list of all entities in EF 5? 如何获取 EF 5 中所有实体的列表?

Entity Framework - Get List of Tables 实体框架 - 获取表列表

var tableNames = context.MetadataWorkspace.GetItems(DataSpace.SSpace)
                        .Select(t => t.Name)
                        .ToList();

We just want to display all the database in a report with EF Core.我们只想在带有 EF Core 的报告中显示所有数据库。 Raw Sql is select * from sys.tables or information_schema.tables , however looking for EFCore way原始 Sql 是select * from sys.tables or information_schema.tables ,但是寻找 EFCore 方式

The list of all tables in the database - no.数据库中所有表的列表 - 没有。 The list of all entities mapped to a context and information about their database mappings (tables, columns, indexes etc.) - sure.映射到上下文的所有实体的列表以及有关它们的数据库映射(表、列、索引等)的信息 - 当然。 You would use the EF Core metadata API which is much more intuitive than EF ones.您将使用比 EF 更直观的 EF Core 元数据 API。 Start with Model property of the DbContext and explore the available (extension) methods.DbContext Model属性开始,探索可用的(扩展)方法。

For instance:例如:

var tableNames = context.Model.GetEntityTypes()
    .Select(t => t.GetTableName())
    .Distinct()
    .ToList();

You could try using the internals of EFCore scaffolding to retrieve all the tables您可以尝试使用 EFCore 脚手架的内部结构来检索所有表

https://github.com/dotnet/efcore/blob/v5.0.7/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs https://github.com/dotnet/efcore/blob/v5.0.7/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs

such as the below:例如以下:

var serviceCollection = new ServiceCollection()
                    .AddEntityFrameworkSqlite()
                    .AddLogging()
                    .AddEntityFrameworkDesignTimeServices()
                    .AddSingleton<LoggingDefinitions, SqliteLoggingDefinitions>()                                              
                    .AddSingleton<IDatabaseModelFactory, SqliteDatabaseModelFactory>()                                   
                    .BuildServiceProvider();
    
                var model = serviceCollection.GetRequiredService<IDatabaseModelFactory>();
                var dbOptions = new DatabaseModelFactoryOptions();
                var databaseModelFactory = model.Create(**Put connectionstring here**, dbOptions);

var tables = databaseModelFactory.Tables; //Contains all the tables. 

This currently works with EFCore 5.07 and sqllite not tested this with other providers yet.这目前适用于 EFCore 5.07,而 sqllite 尚未与其他提供者进行测试。 As always, when tapping into internals be warned that upgrading EFCore could break this.与往常一样,在进入内部时请注意升级 EFCore 可能会破坏这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM