diff --git a/docs/研发/重点难点.xmind b/docs/研发/重点难点.xmind new file mode 100644 index 00000000..a8763646 Binary files /dev/null and b/docs/研发/重点难点.xmind differ diff --git a/labs/EFCoreTest/DbContextTest1/.gitignore b/labs/EFCoreTest/DbContextTest1/.gitignore new file mode 100644 index 00000000..988994ff --- /dev/null +++ b/labs/EFCoreTest/DbContextTest1/.gitignore @@ -0,0 +1,16 @@ +*.bak +*.suo +*.db +*.user +.vs +obj +Obj +bin +Bin +debug +Debug +release +Release +Logs +logs +node_modules \ No newline at end of file diff --git a/labs/EFCoreTest/DbContextTest1/DbContext1.cs b/labs/EFCoreTest/DbContextTest1/DbContext1.cs new file mode 100644 index 00000000..cf88b270 --- /dev/null +++ b/labs/EFCoreTest/DbContextTest1/DbContext1.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using System; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage; + +namespace DbContextTest1 +{ + public class Test1 + { + public Guid Id { get; set; } + public string TestField1 { get; set; } + } + public class DbContext1 : DbContext + { + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=data.db;"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(); + + } + } +} diff --git a/labs/EFCoreTest/DbContextTest1/DbContextTest1.csproj b/labs/EFCoreTest/DbContextTest1/DbContextTest1.csproj new file mode 100644 index 00000000..bf238467 --- /dev/null +++ b/labs/EFCoreTest/DbContextTest1/DbContextTest1.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/labs/EFCoreTest/DbContextTest2/.gitignore b/labs/EFCoreTest/DbContextTest2/.gitignore new file mode 100644 index 00000000..988994ff --- /dev/null +++ b/labs/EFCoreTest/DbContextTest2/.gitignore @@ -0,0 +1,16 @@ +*.bak +*.suo +*.db +*.user +.vs +obj +Obj +bin +Bin +debug +Debug +release +Release +Logs +logs +node_modules \ No newline at end of file diff --git a/labs/EFCoreTest/DbContextTest2/DbContext2.cs b/labs/EFCoreTest/DbContextTest2/DbContext2.cs new file mode 100644 index 00000000..db3101a3 --- /dev/null +++ b/labs/EFCoreTest/DbContextTest2/DbContext2.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using System; + +namespace DbContextTest2 +{ + public class Test2 + { + public Guid Id { get; set; } + public string TestField1 { get; set; } + } + + public class DbContext2 : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=data.db;"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(); + } + } +} \ No newline at end of file diff --git a/labs/EFCoreTest/DbContextTest2/DbContextTest2.csproj b/labs/EFCoreTest/DbContextTest2/DbContextTest2.csproj new file mode 100644 index 00000000..5de529de --- /dev/null +++ b/labs/EFCoreTest/DbContextTest2/DbContextTest2.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/labs/EFCoreTest/EFCoreTest.sln b/labs/EFCoreTest/EFCoreTest.sln index e05c74d6..3b0f561e 100644 --- a/labs/EFCoreTest/EFCoreTest.sln +++ b/labs/EFCoreTest/EFCoreTest.sln @@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29814.53 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreTest", "EFCoreTest.csproj", "{4BB784D0-B6BB-4450-9E34-F64EA74B5555}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCoreTest", "EFCoreTest\EFCoreTest.csproj", "{4BB784D0-B6BB-4450-9E34-F64EA74B5555}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextTest1", "DbContextTest1\DbContextTest1.csproj", "{CF862BF8-BFE3-400A-9E51-258FEA107DDB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextTest2", "DbContextTest2\DbContextTest2.csproj", "{F0676659-C83E-46BB-BBB1-5CFBCD19319B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +19,14 @@ Global {4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Debug|Any CPU.Build.0 = Debug|Any CPU {4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Release|Any CPU.ActiveCfg = Release|Any CPU {4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Release|Any CPU.Build.0 = Release|Any CPU + {CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Release|Any CPU.Build.0 = Release|Any CPU + {F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/labs/EFCoreTest/EFCoreTest/.gitignore b/labs/EFCoreTest/EFCoreTest/.gitignore new file mode 100644 index 00000000..11ee0d93 --- /dev/null +++ b/labs/EFCoreTest/EFCoreTest/.gitignore @@ -0,0 +1,19 @@ +*.bak +*.suo +*.db +*.db-shm +*.db-wal +*.user +.vs +obj +Obj +bin +Bin +debug +Debug +release +Release +Logs +logs +node_modules +cockroach-data \ No newline at end of file diff --git a/labs/EFCoreTest/EFCoreTest.csproj b/labs/EFCoreTest/EFCoreTest/EFCoreTest.csproj similarity index 70% rename from labs/EFCoreTest/EFCoreTest.csproj rename to labs/EFCoreTest/EFCoreTest/EFCoreTest.csproj index 20a7edf4..6bc4b7d3 100644 --- a/labs/EFCoreTest/EFCoreTest.csproj +++ b/labs/EFCoreTest/EFCoreTest/EFCoreTest.csproj @@ -1,4 +1,4 @@ - + Exe @@ -12,4 +12,9 @@ + + + + + diff --git a/labs/EFCoreTest/Entities.cs b/labs/EFCoreTest/EFCoreTest/Entities.cs similarity index 100% rename from labs/EFCoreTest/Entities.cs rename to labs/EFCoreTest/EFCoreTest/Entities.cs diff --git a/labs/EFCoreTest/MyDbContext.cs b/labs/EFCoreTest/EFCoreTest/MyDbContext.cs similarity index 95% rename from labs/EFCoreTest/MyDbContext.cs rename to labs/EFCoreTest/EFCoreTest/MyDbContext.cs index aa1ff565..b8750a9b 100644 --- a/labs/EFCoreTest/MyDbContext.cs +++ b/labs/EFCoreTest/EFCoreTest/MyDbContext.cs @@ -8,7 +8,7 @@ namespace EFCoreTest { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - //optionsBuilder.UseSqlite("Data Source=data.db;"); + optionsBuilder.UseSqlite("Data Source=data.db;"); //optionsBuilder.UseMySql("Server=139.180.143.95;Port=3306;Database=test;Uid=admin;Pwd=admin;"); ///https://www.cockroachlabs.com/docs/stable/build-a-csharp-app-with-cockroachdb.html /// cockroach start --insecure --host=localhost --http-port=9090 @@ -16,7 +16,7 @@ namespace EFCoreTest /// CREATE DATABASE test; /// DROP DATABASE test CASCADE; /// \q - optionsBuilder.UseNpgsql("User ID=root;Host=localhost;Port=26257;Database=test;"); + //optionsBuilder.UseNpgsql("User ID=root;Host=localhost;Port=26257;Database=test;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/labs/EFCoreTest/EFCoreTest/Program.cs b/labs/EFCoreTest/EFCoreTest/Program.cs new file mode 100644 index 00000000..a4d3367f --- /dev/null +++ b/labs/EFCoreTest/EFCoreTest/Program.cs @@ -0,0 +1,72 @@ +using DbContextTest1; +using DbContextTest2; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage; +using System.Linq; + +namespace EFCoreTest +{ + internal class Program + { + private static void Main(string[] args) + { + using (var db = new MyDbContext()) + { + if (db.Database.EnsureCreated()) + { + } + + //var set = db.Set(); + //if (db.Database.EnsureCreated()) + //{ + // //set.Add(new User + // //{ + // // UserName = "test", + // // UserRoles = new List + // // { + // // new UserRole + // // { + // // Role=new Role{ + // // Name="admin" + // // } + // // } + // // } + // //}); + // //db.SaveChanges(); + //} + ////foreach (var item in set.ToList()) + ////{ + //// set.Remove(item); + //// db.SaveChanges(); + ////} + //var user = set.Include(o => o.UserRoles).ThenInclude(o => o.Role).FirstOrDefault(); + //user.UserRoles.FirstOrDefault().Role.Name = "test"; + //db.SaveChanges(); + } + using (var db = new DbContext1()) + { + try + { + db.Set().Any(); + } + catch + { + var databaseCreator = db.GetService(); + databaseCreator.CreateTables(); + } + } + using (var db = new DbContext2()) + { + try + { + db.Set().Any(); + } + catch + { + var databaseCreator = db.GetService(); + databaseCreator.CreateTables(); + } + } + } + } +} \ No newline at end of file diff --git a/labs/EFCoreTest/Program.cs b/labs/EFCoreTest/Program.cs deleted file mode 100644 index 184dc1b3..00000000 --- a/labs/EFCoreTest/Program.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System.Collections.Generic; -using System.Linq; - -namespace EFCoreTest -{ - internal class Program - { - private static void Main(string[] args) - { - using (var db = new MyDbContext()) - { - var set = db.Set(); - if (db.Database.EnsureCreated()) - { - set.Add(new User - { - UserName = "test", - UserRoles = new List - { - new UserRole - { - Role=new Role{ - Name="admin" - } - } - } - }); - db.SaveChanges(); - } - //foreach (var item in set.ToList()) - //{ - // set.Remove(item); - // db.SaveChanges(); - //} - var user = set.Include(o => o.UserRoles).ThenInclude(o => o.Role).FirstOrDefault(); - user.UserRoles.FirstOrDefault().Role.Name = "test"; - db.SaveChanges(); - } - } - } -} \ No newline at end of file