From e1819a42fc6eadc63c82596420f59a0ec6eb3719 Mon Sep 17 00:00:00 2001 From: Zr Date: Sat, 13 Nov 2021 17:30:51 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E5=B9=B6=E6=A0=B9=E6=8D=AE=E6=95=B0=E6=8D=AE=E5=BA=93=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=94=9F=E6=88=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EntifyFrameworkTest.sln | 25 ++++++++++ .../AutoGenModels/EFSqliteContextTest.cs | 43 ++++++++++++++++++ .../AutoGenModels/Student.cs | 21 +++++++++ .../EntifyFrameworkTest.csproj | 20 ++++++++ .../EntifyFrameworkTest/Program.cs | 12 +++++ .../EntifyFrameworkTest/data/TestSqlite.db | Bin 0 -> 8192 bytes .../EntifyFrameworkTest/dbCodeGen.bat | 2 + .../EntifyFrameworkTest/log.txt | 13 ++++++ 8 files changed, 136 insertions(+) create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest.sln create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/EFSqliteContextTest.cs create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/Student.cs create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest/data/TestSqlite.db create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest/dbCodeGen.bat create mode 100644 Project/EntifyFrameworkTest/EntifyFrameworkTest/log.txt diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest.sln b/Project/EntifyFrameworkTest/EntifyFrameworkTest.sln new file mode 100644 index 0000000..00b7421 --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31829.152 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntifyFrameworkTest", "EntifyFrameworkTest\EntifyFrameworkTest.csproj", "{A8B574AD-745A-4CA4-BEF2-A339F0D269D4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A8B574AD-745A-4CA4-BEF2-A339F0D269D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A8B574AD-745A-4CA4-BEF2-A339F0D269D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A8B574AD-745A-4CA4-BEF2-A339F0D269D4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A8B574AD-745A-4CA4-BEF2-A339F0D269D4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {630563EF-C9CB-4EDC-A2D9-4C2762B7C9B8} + EndGlobalSection +EndGlobal diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/EFSqliteContextTest.cs b/Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/EFSqliteContextTest.cs new file mode 100644 index 0000000..a00d30f --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/EFSqliteContextTest.cs @@ -0,0 +1,43 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; + +#nullable disable + +namespace EntityFrameworkCore.AutoGen +{ + public partial class EFSqliteContextTest : DbContext + { + public EFSqliteContextTest() + { + } + + public EFSqliteContextTest(DbContextOptions options) + : base(options) + { + } + + public virtual DbSet Students { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { +#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263. + optionsBuilder.UseSqlite("Filename=data/TestSqlite.db"); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(entity => + { + entity.Property(e => e.Id).ValueGeneratedNever(); + }); + + OnModelCreatingPartial(modelBuilder); + } + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + } +} diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/Student.cs b/Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/Student.cs new file mode 100644 index 0000000..f048f42 --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/AutoGenModels/Student.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; + +#nullable disable + +namespace EntityFrameworkCore.AutoGen +{ + public partial class Student + { + [Key] + [Column("id")] + public long Id { get; set; } + [Column("name")] + public string Name { get; set; } + [Column("age")] + public long? Age { get; set; } + } +} diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj b/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj new file mode 100644 index 0000000..fd04b12 --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj @@ -0,0 +1,20 @@ + + + + Exe + net5.0 + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs new file mode 100644 index 0000000..28850b7 --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs @@ -0,0 +1,12 @@ +using System; +using EntityFrameworkCore.AutoGen; + +namespace EntifyFrameworkTest +{ + class Program + { + static void Main(string[] args) + { + } + } +} diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/data/TestSqlite.db b/Project/EntifyFrameworkTest/EntifyFrameworkTest/data/TestSqlite.db new file mode 100644 index 0000000000000000000000000000000000000000..074e025b1d00aef4335519fed9f7804a6bc5f0fd GIT binary patch literal 8192 zcmeI#y$ZrG5C`z22!c{{b1EE}6vV~R#VSF>TD6T}$BI=7et^aY@R5B3o7#elFQES; z;qHQ7{Kk9F##d?FMZH60HmMsJX zAOHafKmY;|fB*y_009U<;7%V0WpgNSA_qPBUcW7j(7 fZ=QYn5fFd?1Rwwb2tWV=5P$##AOHaf{II|aHhL|H literal 0 HcmV?d00001 diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/dbCodeGen.bat b/Project/EntifyFrameworkTest/EntifyFrameworkTest/dbCodeGen.bat new file mode 100644 index 0000000..d9ce3ba --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/dbCodeGen.bat @@ -0,0 +1,2 @@ +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef dbcontext scaffold "Filename=data/TestSqlite.db" Microsoft.EntityFrameworkCore.Sqlite --table Students --output-dir AutoGenModels --namespace EntityFrameworkCore.AutoGen --data-annotations --context EFSqliteContextTest diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/log.txt b/Project/EntifyFrameworkTest/EntifyFrameworkTest/log.txt new file mode 100644 index 0000000..f18c809 --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/log.txt @@ -0,0 +1,13 @@ +1、.net core后没有在SDK安装dotnet-ef,进行手动安装: + dotnet tool install --global dotnet-ef + +2、在.csproj中加入Design(也可以通过NuGet): + dotnet add package Microsoft.EntityFrameworkCore.Design + +3、输入命令进行生成: + dotnet ef dbcontext scaffold "Filename=data/TestSqlite.db" Microsoft.EntityFrameworkCore.Sqlite --table Students --output-dir AutoGenModels --namespace EntityFrameworkCore.AutoGen --data-annotations --context EFSqliteContextTest + +Ps: + --table 可以加入多个,如果一个没有加入,则生成所有的table; + --data-annotations 使用数据注解和Fluent API + --context EFSqliteContextTest 重命名上下文的Context -- Gitee From 3bd35164d6d760cdc360a45558ad9245cefd1a17 Mon Sep 17 00:00:00 2001 From: Zr Date: Sat, 13 Nov 2021 17:43:59 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A1=A8=E5=A2=9E=E5=8A=A0=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EntifyFrameworkTest.csproj | 6 ++++++ .../EntifyFrameworkTest/Program.cs | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj b/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj index fd04b12..e06d9bb 100644 --- a/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj @@ -17,4 +17,10 @@ + + + Always + + + diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs index 28850b7..5b4da23 100644 --- a/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs @@ -3,10 +3,23 @@ using EntityFrameworkCore.AutoGen; namespace EntifyFrameworkTest { - class Program + internal class Program { - static void Main(string[] args) + private static bool AddStudent(long id, string name, long? age) { + using (var db = new EFSqliteContextTest()) + { + var newStudent = new Student {Id = id, Age = age, Name = name}; + db.Students.Add(newStudent); + var affected = db.SaveChanges(); + return affected == 1; + } + } + + private static void Main(string[] args) + { + var ok = AddStudent(0, "小红", 12); + Console.WriteLine(ok); } } -} +} \ No newline at end of file -- Gitee From 3d2e609f0788117b1c76d44c45fc65a96393420b Mon Sep 17 00:00:00 2001 From: Zr Date: Sat, 13 Nov 2021 18:10:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EntifyFrameworkTest/Program.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs index 5b4da23..7ec0054 100644 --- a/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs @@ -1,4 +1,7 @@ using System; +using System.Collections; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using EntityFrameworkCore.AutoGen; namespace EntifyFrameworkTest @@ -16,10 +19,24 @@ namespace EntifyFrameworkTest } } + static int RemoveStudent(long id) + { + using (var db=new EFSqliteContextTest()) + { + var tempStudent = db.Students.Where(p => p.Id == id); + db.Students.RemoveRange(tempStudent); + + var affected = db.SaveChanges(); + return affected; + } + } + private static void Main(string[] args) { - var ok = AddStudent(0, "小红", 12); - Console.WriteLine(ok); + // var ok = AddStudent(0, "小红", 12); + // Console.WriteLine(ok); + var affect = RemoveStudent(0); + Console.WriteLine(affect); } } } \ No newline at end of file -- Gitee