entityframework

main
syneffort 2 years ago
parent 8a3eb1fe6f
commit dd8bb2667c
  1. 15
      PacticeSolution/EntityFrameworkSample/EntityFrameworkSample.csproj
  2. 58
      PacticeSolution/EntityFrameworkSample/Program.cs
  3. 16
      PacticeSolution/EntityFrameworkSample/User.cs
  4. 20
      PacticeSolution/EntityFrameworkSample/UserDbContext.cs
  5. 1
      PacticeSolution/LinqInNotIn/Program.cs
  6. 6
      PacticeSolution/PacticeSolution.sln

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
</ItemGroup>
</Project>

@ -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}");
}
}
}
}
}

@ -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; }
}
}

@ -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> 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);
}
}
}

@ -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}");

@ -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

Loading…
Cancel
Save