efcore 一对一 一对多 多对多关系
one to one
- 用户
public class User : Entity
{
[Required]
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public DateTime Birthday { get; set; }
public virtual Address Address { get; set; }
}
- 居住地址
public class Address : Entity
{
[Required]
public string AddressName { get; set; }
public string AddressLine { get; set; }
public virtual User UserNagation { get; set; }
}
one to many
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace Cy.SmartHospital.QueueSystem.Domain.Model
{
/// <summary>
/// 队列详情
/// </summary>
[Table("QueueItem")]
public class QueueItem : Entity
{
/// <summary>
/// 就诊状态
/// </summary>
public int? QueueStatus { get; set; }
/// <summary>
/// 排队编号
/// </summary>
[StringLength(8)]
public string QueueNumber { get; set; }
/// <summary>
/// 排队顺序
/// </summary>
public double? QueueOrder { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime? CreateTime { get; set; }
/// <summary>
/// 接诊人员类别
/// </summary>
public int? HandleType { get; set; }
/// <summary>
/// 接诊人员id
/// </summary>
[Required]
[StringLength(32)]
public string HandlerId { get; set; }
/// <summary>
/// 最后修改时间
/// </summary>
public DateTime? LastTime { get; set; }
/// <summary>
/// 挂号信息
/// </summary>
public virtual string RegisterInfoId { get; set; }
/// <summary>
/// 挂号信息
/// </summary>
public virtual RegisterInfo RegisterInfo { get; set; }
public virtual string QueueInfoId { get; set; }
/// <summary>
/// 队列信息
/// </summary>
public virtual QueueInfo QueueInfo { get; set; }
/// <summary>
/// 排队人员标签属性
/// </summary>
public ICollection<QueueItemPersonTag> QueueItemPersonTags { get; set; }
}
}
- 说明
队列详情中存在挂号信息表外键
- 设计
//队列详情->挂号信息。
modelBuilder.Entity<QueueItem>() //主表
.HasOne<RegisterInfo>(q => q.RegisterInfo) //外键实体
.WithMany(r=>r.QueueItems)
.HasForeignKey(r => r.RegisterInfoId); //外键
//队列信息->队列详情
modelBuilder.Entity<QueueItem>()
.HasOne<QueueInfo>(q => q.QueueInfo)
.WithMany(q => q.QueueItems)
.HasForeignKey(r => r.QueueInfoId);
many to many
- users
public class User : Entity
{
[Required]
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public DateTime Birthday { get; set; }
/// <summary>
/// 用户所拥有的角色
/// </summary>
public virtual IList<UserRole> UserRoles { get; set; }
/// <summary>
/// 所拥有的权限
/// </summary>
public virtual IList<UserPermission> UserPermissions { get; set; }
}
中间表UserRoles是一个集合
- roles
public class Role:Entity
{
[Required]
public string Name { get; set; }
/// <summary>
/// 角色中所拥有的用户
/// </summary>
public IList<UserRole> UserRoles { get; set; }
/// <summary>
/// 所拥有的权限
/// </summary>
public IList<RolePermission> RolePermissions { get; set; }
}
- userrole
public class UserRole
{
/// <summary>
/// 用户Id
/// </summary>
public string UserId { get; set; }
public User User { get; set; }
/// <summary>
/// 角色Id
/// </summary>
public string RoleId { get; set; }
public Role Role { get; set; }
}
- map
modelBuilder.Entity<UserRole>()
.HasKey(ur => new { ur.UserId, ur.RoleId });
modelBuilder.Entity<UserRole>()
.HasOne<User>(ur => ur.User)
.WithMany(u => u.UserRoles)
.HasForeignKey(ur => ur.UserId);
modelBuilder.Entity<UserRole>()
.HasOne<Role>(ur => ur.Role)
.WithMany(u => u.UserRoles)
.HasForeignKey(ur => ur.RoleId);
WithMany中使用中间表