c# entity framework core树状结构增删改查
2021-03-11 08:31
标签:done console sep new efault created active tip ring 首先创建一个.net core控制台程序,添加实体类 c# entity framework core树状结构增删改查 标签:done console sep new efault created active tip ring 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12640506.html实体类:Employee
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace TreeEntityDemo
{
///
定义DbContext
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.IO;
namespace TreeEntityDemo
{
public class AppDbContext:DbContext
{
public AppDbContext()
{
}
public AppDbContext(DbContextOptions options):base(options)
{
}
public DbSet
配置数据库链接:appsettings.json
{
"ConnectionStrings": {
"TreeDemoDb": "Server=(localdb)\\MSSQLLocalDB;Database=TreeDemoDb;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
添加数据库迁移 Add-Migration
add-migration InitialCreate
在Program.cs中测试
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace TreeEntityDemo
{
class Program
{
static void Main(string[] args)
{
using (var context = new AppDbContext())
{
context.Database.EnsureCreated();
if (!context.Employees.Any())
{
context.Employees.AddRange(
new Employee()
{
Name = "Manager1"
},
new Employee()
{
Name = "Employee1"
},
new Employee()
{
Name = "Employee2"
},
new Employee()
{
Name = "Employee3"
}
);
}
context.SaveChanges();
}
//GetParent();
SetParent();
GetChildren();
Console.WriteLine("Done!");
}
public static void Find()
{
using (var context = new AppDbContext())
{
var employees = context.Employees.FirstOrDefault(t => t.Name == "Employee1");
Console.WriteLine(employees.Name);
Console.WriteLine(employees.Manager.Name);
}
}
///
文章标题:c# entity framework core树状结构增删改查
文章链接:http://soscw.com/index.php/essay/63124.html