From dd8bb2667c0fdfc1a4d43ec1b4342e2d0e5c8c7c Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 3 Aug 2023 14:04:59 +0900 Subject: [PATCH] entityframework --- .../EntityFrameworkSample.csproj | 15 +++++ .../EntityFrameworkSample/Program.cs | 58 +++++++++++++++++++ PacticeSolution/EntityFrameworkSample/User.cs | 16 +++++ .../EntityFrameworkSample/UserDbContext.cs | 20 +++++++ PacticeSolution/LinqInNotIn/Program.cs | 1 + PacticeSolution/PacticeSolution.sln | 6 ++ 6 files changed, 116 insertions(+) create mode 100644 PacticeSolution/EntityFrameworkSample/EntityFrameworkSample.csproj create mode 100644 PacticeSolution/EntityFrameworkSample/Program.cs create mode 100644 PacticeSolution/EntityFrameworkSample/User.cs create mode 100644 PacticeSolution/EntityFrameworkSample/UserDbContext.cs diff --git a/PacticeSolution/EntityFrameworkSample/EntityFrameworkSample.csproj b/PacticeSolution/EntityFrameworkSample/EntityFrameworkSample.csproj new file mode 100644 index 0000000..d60fd6a --- /dev/null +++ b/PacticeSolution/EntityFrameworkSample/EntityFrameworkSample.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/PacticeSolution/EntityFrameworkSample/Program.cs b/PacticeSolution/EntityFrameworkSample/Program.cs new file mode 100644 index 0000000..4e6d3f7 --- /dev/null +++ b/PacticeSolution/EntityFrameworkSample/Program.cs @@ -0,0 +1,58 @@ +using System.Diagnostics; + +namespace EntityFrameworkSample +{ + internal class Program + { + static void Main(string[] args) + { + using (var db = new UserDbContext()) + { + Console.WriteLine("Select *"); + var userList = db.User.ToList(); + foreach (var user in userList) + { + Console.WriteLine($"[{user.Id}] {user.Name} ({user.Phone}) {user.Address}"); + } + + db.User.Add(new User { Name = "Salmon", Phone = "010-1234-5678", Address = "Wild mountain" }); + db.SaveChanges(); + + Console.WriteLine("Select *"); + userList = db.User.ToList(); + foreach (var user in userList) + { + Console.WriteLine($"[{user.Id}] {user.Name} ({user.Phone}) {user.Address}"); + } + + var selected = db.User.Where(u => u.Name == "Torr").FirstOrDefault(); + if (selected != null) + { + selected.Address = "Asgard"; + db.SaveChanges(); + } + + Console.WriteLine("Select *"); + userList = db.User.ToList(); + foreach (var user in userList) + { + Console.WriteLine($"[{user.Id}] {user.Name} ({user.Phone}) {user.Address}"); + } + + selected = db.User.Where(u => u.Name == "Salmon").FirstOrDefault(); + if (selected != null) + { + db.User.Remove(selected); + db.SaveChanges(); + } + + Console.WriteLine("Select *"); + userList = db.User.ToList(); + foreach (var user in userList) + { + Console.WriteLine($"[{user.Id}] {user.Name} ({user.Phone}) {user.Address}"); + } + } + } + } +} \ No newline at end of file diff --git a/PacticeSolution/EntityFrameworkSample/User.cs b/PacticeSolution/EntityFrameworkSample/User.cs new file mode 100644 index 0000000..414c85b --- /dev/null +++ b/PacticeSolution/EntityFrameworkSample/User.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFrameworkSample +{ + internal class User + { + public int Id { get; set; } + public string Name { get; set; } + public string Phone { get; set; } + public string Address { get; set; } + } +} diff --git a/PacticeSolution/EntityFrameworkSample/UserDbContext.cs b/PacticeSolution/EntityFrameworkSample/UserDbContext.cs new file mode 100644 index 0000000..c487f30 --- /dev/null +++ b/PacticeSolution/EntityFrameworkSample/UserDbContext.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFrameworkSample +{ + internal class UserDbContext : DbContext + { + public DbSet User { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + string connectionString = @"Data Source=peacecloud.synology.me,21433;Initial Catalog=Study;User ID=study;Password=Study1234;Encrypt=false;"; + optionsBuilder.UseSqlServer(connectionString); + } + } +} diff --git a/PacticeSolution/LinqInNotIn/Program.cs b/PacticeSolution/LinqInNotIn/Program.cs index 64f5da4..d348c67 100644 --- a/PacticeSolution/LinqInNotIn/Program.cs +++ b/PacticeSolution/LinqInNotIn/Program.cs @@ -34,6 +34,7 @@ Console.WriteLine($"idx: {item.ProductId}, name: {item.Name}"); } + Console.WriteLine("first, single"); var firstItem = products.First(item => item.ProductId == 3); var singleItem = products.Single(item => item.ProductId == 3); Console.WriteLine($"first == single: {firstItem == singleItem}"); diff --git a/PacticeSolution/PacticeSolution.sln b/PacticeSolution/PacticeSolution.sln index 2143681..7836a84 100644 --- a/PacticeSolution/PacticeSolution.sln +++ b/PacticeSolution/PacticeSolution.sln @@ -123,6 +123,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandSample", "CommandSam EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqInNotIn", "LinqInNotIn\LinqInNotIn.csproj", "{9261D8E1-5DEE-4329-AB32-946AD5E69C66}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkSample", "EntityFrameworkSample\EntityFrameworkSample.csproj", "{AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -369,6 +371,10 @@ Global {9261D8E1-5DEE-4329-AB32-946AD5E69C66}.Debug|Any CPU.Build.0 = Debug|Any CPU {9261D8E1-5DEE-4329-AB32-946AD5E69C66}.Release|Any CPU.ActiveCfg = Release|Any CPU {9261D8E1-5DEE-4329-AB32-946AD5E69C66}.Release|Any CPU.Build.0 = Release|Any CPU + {AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE