diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest.sln b/Project/EntifyFrameworkTest/EntifyFrameworkTest.sln new file mode 100644 index 0000000000000000000000000000000000000000..00b742123150450aa35e61b50ea5591f30c9959b --- /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 0000000000000000000000000000000000000000..a00d30f9a687ff80eba995c0bcb0688d720eb099 --- /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 0000000000000000000000000000000000000000..f048f4287fd92a73179d850ccedf1c1fb1671cec --- /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 0000000000000000000000000000000000000000..e06d9bb60f25cd4394ff5c29ba21a3801f1312da --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/EntifyFrameworkTest.csproj @@ -0,0 +1,26 @@ + + + + Exe + net5.0 + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + Always + + + + diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..7ec0054877d61c3bd1e26b4156b48359d5118e49 --- /dev/null +++ b/Project/EntifyFrameworkTest/EntifyFrameworkTest/Program.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using EntityFrameworkCore.AutoGen; + +namespace EntifyFrameworkTest +{ + internal class Program + { + 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; + } + } + + 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 affect = RemoveStudent(0); + Console.WriteLine(affect); + } + } +} \ No newline at end of file diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/data/TestSqlite.db b/Project/EntifyFrameworkTest/EntifyFrameworkTest/data/TestSqlite.db new file mode 100644 index 0000000000000000000000000000000000000000..074e025b1d00aef4335519fed9f7804a6bc5f0fd Binary files /dev/null and b/Project/EntifyFrameworkTest/EntifyFrameworkTest/data/TestSqlite.db differ diff --git a/Project/EntifyFrameworkTest/EntifyFrameworkTest/dbCodeGen.bat b/Project/EntifyFrameworkTest/EntifyFrameworkTest/dbCodeGen.bat new file mode 100644 index 0000000000000000000000000000000000000000..d9ce3ba48773fa6eeceb02670b88f3bc19cacc81 --- /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 0000000000000000000000000000000000000000..f18c8099f2e635ab51dee0675a51860b96d00bd2 --- /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