简体   繁体   English

EntityFrameworkCore DatabaseFirst 的一般使用方法,不在 C#.NET 中搭建脚手架

[英]General usage approach for EntityFrameworkCore DatabaseFirst by not scaffolding in C#.NET

I would like to deliver a C#.NET Core 6 application using EntityFrameworkCore 6 (EF6.Core) to a remote user, while the main intention of the application is to browse the entire MySQL server after authenticating the app to the MySQL server of the user for databases and tables including the records.我想使用 EntityFrameworkCore 6 (EF6.Core) 向远程用户交付一个 C#.NET Core 6 应用程序,而该应用程序的主要目的是在将应用程序验证到用户的 MySQL 服务器后浏览整个 MySQL 服务器用于包括记录的数据库和表。 The app should parse and list all available databases,then the user should select any of the listed databases and then the app should list all tables inside the database including the tables schema, once a table is selected the table records should be parsed and be available for further processing.应用程序应解析并列出所有可用的数据库,然后用户应 select 任何列出的数据库,然后应用程序应列出数据库内的所有表,包括表架构,一旦选择表,表记录应被解析并可用用于进一步处理。

As I know there are some solutions recommending CLI methods such as 'dotnet scaffolding' or 'dotnet migrations' that I do find them not applicable to my case since following conditions are not fulfilled.据我所知,有一些解决方案推荐了 CLI 方法,例如“dotnet 脚手架”或“dotnet 迁移”,我发现它们不适用于我的案例,因为以下条件不满足。

My requirements are (1) The user only gets the application executable and will not share the database structure and tables/schemas with the developer for integration.我的要求是(1)用户只获得应用程序可执行文件,不会与开发人员共享数据库结构和表/模式以进行集成。

(2) The user should not and will not execute the 'dotnet scaffolding' related commands on his machine to hand out the developer the schema of the database. (2) 用户不应该也不会在自己的机器上执行“dotnet scaffolding”相关命令,将数据库的schema交给开发者。

(3) The solution should be general and not specific since the application executable should not change and get tweaked for the use cases (3) 解决方案应该是通用的,而不是具体的,因为应用程序可执行文件不应该改变并针对用例进行调整

Firstly, I would like to know whether EntityFrameworkCore 6 is advanced enough to meet the requirements or not.首先,我想知道 EntityFrameworkCore 6 是否足够先进以满足要求。

Secondly any minimal runnable working C# code that could show how the task should be done is really appreciated.其次,非常感谢任何可以显示任务应该如何完成的最小可运行工作 C# 代码。

Please consider that I am beginner with EF and I would like to read and understand a simple minimum runnable code that can shed some light on how to solve the problem.请考虑我是 EF 的初学者,我想阅读和理解一个简单的最小可运行代码,它可以阐明如何解决问题。

By the way if EF6 is unable to accomplish the requirements is there any other suggested solution available?顺便说一句,如果 EF6 无法满足要求,是否还有其他可用的建议解决方案?

The purpose of Entity Framework Core is to build a code model for a known database. Entity Framework Core 的目的是为已知数据库构建代码 model。 Thus, it is a very poor fit for your application.因此,它非常不适合您的应用程序。

Instead, just use regular ADO.NET methods with a library such as MySqlConnector .相反,只需将常规 ADO.NET 方法与诸如MySqlConnector之类的库一起使用。 You can use the DbConnection.GetSchema method to get a list of all schemas and tables (that your MySQL user account has permission to view).您可以使用DbConnection.GetSchema方法获取所有模式和表的列表(您的 MySQL 用户帐户有权查看)。

using var connection = new MySqlConnection("Server=...;User=...");
connection.Open();

var databases = connection.GetSchema("DATABASES");
var tables = connection.GetSchema("TABLES");

// process the two DataTables retrieved above...

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

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