From 429ca4dd6dde2ee499a0b48a157f07646b32bc8f Mon Sep 17 00:00:00 2001 From: hang <87227557@qq.com> Date: Mon, 9 Sep 2024 00:07:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=B4=E6=97=B6=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.Test/IRaCIS.Core.Test.csproj | 4 ++++ IRaCIS.Core.Test/Program.cs | 14 +++++++------- IRaCIS.Core.Test/TempContext.cs | 12 ++++++++++++ IRaCIS.Core.Test/模板使用说明 | 6 +++--- 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 IRaCIS.Core.Test/TempContext.cs diff --git a/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj b/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj index 317e5cc34..9c1833157 100644 --- a/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj +++ b/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj @@ -1,4 +1,8 @@  + + utf-8 + utf-8 + enable diff --git a/IRaCIS.Core.Test/Program.cs b/IRaCIS.Core.Test/Program.cs index 0a65890ae..90e84d66e 100644 --- a/IRaCIS.Core.Test/Program.cs +++ b/IRaCIS.Core.Test/Program.cs @@ -1,6 +1,7 @@  using Fluid; using IRaCIS.Core.Infra.EFCore; +using IRaCIS.Core.Test.GenerateFolder; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using System; @@ -12,7 +13,7 @@ using System.Text; partial class Program { - static void Main() + static void Main(string[] args) { //发布的目录和项目的目录有区别的 var rootPath = AppContext.BaseDirectory.Replace(@"\bin\net8.0\", "").Replace(@"\bin\Release\net8.0\", ""); @@ -53,10 +54,8 @@ partial class Program #endregion - - // 要生成的表名数组 - var tableNames = new List { "Subject", "Order" }; + var tableNames = new List(args); try { @@ -238,10 +237,11 @@ partial class Program public void CheckCodeFirst() { - var contextOptions = new DbContextOptionsBuilder() - .UseSqlServer(@"Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true").Options; + //var contextOptions = new DbContextOptionsBuilder() + // .UseSqlServer(@"Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true").Options; - using var dbContext = new IRaCISDBContext(contextOptions); + //using var dbContext = new TempContext(contextOptions); + using var dbContext = new TempContext(); var dbModel = dbContext.Model; diff --git a/IRaCIS.Core.Test/TempContext.cs b/IRaCIS.Core.Test/TempContext.cs new file mode 100644 index 000000000..381dfe841 --- /dev/null +++ b/IRaCIS.Core.Test/TempContext.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; + +namespace IRaCIS.Core.Test.GenerateFolder; + +/// +/// 必须要,因为Program 依赖反向工程生成的dbcontext 实例化,获取数据库模型 +/// +public partial class TempContext : DbContext +{ +} diff --git a/IRaCIS.Core.Test/模板使用说明 b/IRaCIS.Core.Test/模板使用说明 index 3702e1731..7117a6510 100644 --- a/IRaCIS.Core.Test/模板使用说明 +++ b/IRaCIS.Core.Test/模板使用说明 @@ -26,12 +26,12 @@ Scaffold-DbContext "连接字符串" 数据库提供程序包 -Project 项目名称 -OutputDir 输出目录 -Context "生成的上下文名称" -DataAnnotations -Tables 表1, 表2 方式1: -Scaffold-DbContext "Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true" Microsoft.EntityFrameworkCore.SqlServer -Project IRaCIS.Core.Test -OutputDir ModelFolder -Context "testContext" -DataAnnotations -Tables Subject, SubjectVisit +Scaffold-DbContext "Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true" Microsoft.EntityFrameworkCore.SqlServer -Project IRaCIS.Core.Test -OutputDir GenerateFolder -Context "TempContext" -DataAnnotations -Tables Dictionary, Site 方式2: 多个表 -t Subject -t SubjectVisit -dotnet ef dbcontext scaffold "Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true" Microsoft.EntityFrameworkCore.SqlServer -p IRaCIS.Core.Test -d -o ModelFolder -c "IRaCISDBContext" -t Subject +dotnet ef dbcontext scaffold "Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true" Microsoft.EntityFrameworkCore.SqlServer -p IRaCIS.Core.Test -d -o GenerateFolder -c "TempContext" -t Dictionary -t Site 开发者PowerShell || 程序包管理控制台命令行 使用: 通过Liquid模板生成基础视图模型、服务接口、服务实现基本方法(必须先有数据库实体模型,否则视图模型无法生成) - dotnet run --project IRaCIS.Core.Test \ No newline at end of file + dotnet run --project IRaCIS.Core.Test -- Dictionary Site \ No newline at end of file