EntityFramework - 在控制台应用程序中实现脚手架

问题描述 投票:0回答:1

我有一个控制台应用程序(使用 .NET 8 创建)。 该应用程序应该执行SQL server数据库的脚手架(逆向工程)。 由于某些原因,我无法通过命令行使用脚手架。因此我想使用 提到控制台应用程序。 在我的控制台应用程序项目中,我引用了以下 Nuget 包:

Microsoft.EntityFrameworkCore.Design Version=8.0.4
Microsoft.EntityFrameworkCore.SqlServer Version=8.0.4
Microsoft.EntityFrameworkCore.Tools Version=8.0.4

Unfortunately, I can't find any code examples of how to program the scaffolding in C#.
(parameterization and executing the scaffolding).

I would be very happy if one of you could show me the way how I can do.

With kind regards
Christian

I tries a lot of research in the internet but found no example.
c# entity-framework-core console-application
1个回答
0
投票

我想出了一个基本的脚手架,您可以对其进行修改以在您的应用程序中使用

基本脚手架:

using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.Extensions.DependencyInjection;

public static class DatabaseScaffolder
{
    public static void ScaffoldDatabase(string connectionString, string outputPath, string[] tables = null, string contextName = "DbContext")
    {
        // Set up options for scaffolding
        var options = new ReverseEngineerOptions
        {
            ConnectionString = connectionString,
            ContextName = contextName,
            ProjectPath = outputPath,
            ProjectRootNamespace = "YourNamespace",  // Update this to your actual namespace
            Tables = tables ?? new string[] { },
            UseDatabaseNames = true
        };

        // Create a service collection to hold the services needed for scaffolding
        var serviceCollection = new ServiceCollection();
        ConfigureServices(serviceCollection);

        // Resolve the scaffolding service
        var serviceProvider = serviceCollection.BuildServiceProvider();
        var scaffolder = serviceProvider.GetRequiredService<IReverseEngineerScaffolder>();

        // Perform scaffolding
        var scaffoldedModel = scaffolder.ScaffoldModel(
            options.ConnectionString,
            new DatabaseModelFactoryOptions(tables, schemas: null),
            new ModelReverseEngineerOptions(),
            new ModelCodeGenerationOptions
            {
                ContextName = options.ContextName,
                ContextNamespace = null,
                ModelNamespace = null,
                UseDataAnnotations = false,
                SuppressConnectionStringWarning = true,
                Language = "C#",
                RootNamespace = null,
                ConnectionString = options.ConnectionString
            });

        // Save the scaffolded context and entity types to the specified output path
        scaffolder.Save(
            scaffoldedModel,
            outputPath,
            overwriteFiles: true);
    }

    private static void ConfigureServices(IServiceCollection services)
    {
        // Add EF Core Design services
        new DesignTimeServicesBuilder()
            .AddDesignTimeServices(services);
    }
}

用法示例:

static void Main(string[] args)
{
    string connectionString = "Server=your_server;Database=your_database;Trusted_Connection=True;";
    string outputPath = @"path\to\output";  // Specify the output directory
    string[] tables = { "Table1", "Table2" };  // Specify tables to scaffold, or null to scaffold all

    DatabaseScaffolder.ScaffoldDatabase(connectionString, outputPath, tables, "YourDbContext");
}

请调整方法以满足您的需求,这应该会给您一个基本的想法......

© www.soinside.com 2019 - 2024. All rights reserved.