创建.net core 控制台应用程序:
项目文件结构:
添加如下NuGet包:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
添加如下实体类:
///
/// 部门
///
public class Dept
{
private Dept() { }
///
/// 单独添加部门
///
///
///
public Dept(string name,long? parentId)
{
Name = name;
ParentId = parentId;
}
///
/// 添加部门并添加用户
///
///
///
///
public Dept(string name, long? parentId, ICollection userDepts)
{
Name = name;
ParentId = parentId;
UserDepts = userDepts;
}
public long Id { get; private set; }
public string Name { get; private set; }
public long? ParentId { get; private set; }
public virtual Dept Parent { get; private set; }
public virtual ICollection Depts { get; private set; }
public virtual ICollection UserDepts { get; private set; }
}
///
/// 用户
///
public class User
{
private User() { }
public User(string name, string phone, int sex)
{
Name = name;
Phone = phone;
Sex = sex;
}
public User(long id,string name, string phone, int sex)
{
Name = name;
Phone = phone;
Sex = sex;
Id = id;
}
public User(string name, string phone, int sex, ICollection userDepts, ICollection userCompanys)
{
Name = name;
Phone = phone;
Sex = sex;
UserDepts = userDepts;
UserCompanys = userCompanys;
}
public User(long id,string name, string phone, int sex, ICollection userDepts, ICollection userCompanys)
{
Name = name;
Phone = phone;
Sex = sex;
UserDepts = userDepts;
UserCompanys = userCompanys;
Id = id;
}
public long Id { get; private set; }
public string Name { get; private set; }
public string Phone { get; private set; }
public int Sex { get; private set; }
public virtual ICollection UserDepts { get; private set; }
public virtual ICollection UserCompanys { get; private set; }
}
///
/// 用户公司
///
public class UserCompany
{
private UserCompany() { }
///
/// 通过用户添加公司
///
///
public UserCompany(string name)
{
Name = name;
}
///
/// 单独为用户添加公司
///
///
///
public UserCompany(string name, long userId)
{
Name = name;
UserId = userId;
}
///
/// 通过公司添加用户
///
///
///
public UserCompany(string name, User user)
{
Name = name;
User = user;
}
public long Id { get; private set; }
public string Name { get; private set; }
public long UserId { get; private set; }
public virtual User User { get; private set; }
}
///
/// 用户部门
///
public class UserDept
{
private UserDept() { }
public UserDept(long? userId, long? deptId)
{
if (userId.HasValue)
UserId = userId.Value;
if (deptId.HasValue)
DeptId = deptId.Value;
}
public long Id { get; private set; }
public long UserId { get; private set; }
public virtual User User { get; private set; }
public long DeptId { get; private set; }
public virtual Dept Dept { get; private set; }
}
添加DbContext:
public class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-2H9332M;Initial Catalog=Test;Integrated Security=True");
}
public DbSet Users { get; set; }
public DbSet Depts { get; set; }
public DbSet UserCompanys { get; set; }
public DbSet UserDepts { get; set; }
}
在程序包控件管理台分别执行如下命令:
migrations add init
update-database
执行成功后生成的数据库关系图如下:
在Program类中添加如下测试代码:
class Program
{
static void Main(string[] args)
{
var context = new TestContext();
//添加部门
context.Set().Add(new Dept("部门1", null));
//添加用户
context.Set().Add(new User("用户1", "1111111", 0));
//添加公司
context.Set().Add(new UserCompany("公司1", 1));
//添加部门、用户部门
context.Set().Add(new Dept(
"部门2",
1,
new List()
{
new UserDept(1,null)
}));
//添加用户、用户部门、用户公司
context.Set().Add(new User(
"张三",
"13037318682",
1,
new List
{
new UserDept(null, 1)
},
new List
{
new UserCompany("ABC公司")
}));
//添加用户、公司
context.Set().Add(new UserCompany("公司2", new User("用户2", "222222", 2)));
context.SaveChanges();
}
}
运行代码添加的数据如下:
接着我们来测试下一对多的更新代码如下:
//更新用户、用户部门、用户公司
context.Set().Update(new User(1, "李四", "13037318682", 1,
new List {
new UserDept(null,2)
},
new List
{
new UserCompany("ABC公司修改")
}));
context.SaveChanges();
结果如下:
使用这种方式修改会在UserDept、UserCompany表中新增一条数据,这种结果也正是我们业务所需要的。
如果我想修改一对多中的子表某一条记录而不是增加一条数据又该如何操作呢?请看如下代码:
context.Set().Update(new User(2, "用户2修改2", "13037318682", 1,
new List {
new UserDept(2,2,2)
},
new List
{
new UserCompany(1,"ABCD公司修改")
}));
context.SaveChanges();
结果如下:
由此可以看出在使用修改操作时需要指定主键Id。