efcore 一对一 一对多 多对多关系

文章目录

  • one to one
  • one to many
  • many to many

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
{
    /// 
    /// 队列详情
    /// 
    [Table("QueueItem")]
    public class QueueItem : Entity
    {
        /// 
        /// 就诊状态
        /// 
        public int? QueueStatus { get; set; }
        /// 
        /// 排队编号
        /// 
        [StringLength(8)]
        public string QueueNumber { get; set; }

        /// 
        /// 排队顺序
        /// 
        public double? QueueOrder { get; set; }
        /// 
        /// 创建时间
        /// 
        public DateTime? CreateTime { get; set; }

        /// 
        /// 接诊人员类别
        /// 
        public int? HandleType { get; set; }

        /// 
        /// 接诊人员id
        /// 
        [Required]
        [StringLength(32)]
        public string HandlerId { get; set; }
        /// 
        /// 最后修改时间
        /// 
        public DateTime? LastTime { get; set; }

        /// 
        /// 挂号信息
        /// 
        public virtual string RegisterInfoId { get; set; }
        /// 
        /// 挂号信息
        /// 
        public virtual RegisterInfo RegisterInfo { get; set; }
       
        public virtual string QueueInfoId { get; set; }
        /// 
        /// 队列信息
        /// 
        public virtual QueueInfo QueueInfo { get; set; } 

        /// 
        /// 排队人员标签属性
        /// 
        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; }
        /// 
        /// 用户所拥有的角色
        /// 
        public virtual IList<UserRole> UserRoles { get; set; }
        
        /// 
        /// 所拥有的权限
        /// 
        public virtual IList<UserPermission> UserPermissions { get; set; }
    }

中间表UserRoles是一个集合

  • roles
    public class Role:Entity
    {

        [Required]
        public string Name { get; set; }
        
        /// 
        /// 角色中所拥有的用户
        /// 
        public IList UserRoles { get; set; }
        
        /// 
        /// 所拥有的权限
        /// 
        public IList RolePermissions { get; set; }
    }
  • userrole
    public class UserRole
    {
        /// 
        /// 用户Id
        /// 
        public string UserId { get; set; }

        public User User { get; set; }

        /// 
        /// 角色Id
        /// 
        public string RoleId { get; set; }
        public Role Role { get; set; }


    }
  • map
 modelBuilder.Entity()
                .HasKey(ur => new { ur.UserId, ur.RoleId });             
                
modelBuilder.Entity()
    .HasOne(ur => ur.User)
    .WithMany(u => u.UserRoles)
    .HasForeignKey(ur => ur.UserId);

modelBuilder.Entity()
    .HasOne(ur => ur.Role)
    .WithMany(u => u.UserRoles)
    .HasForeignKey(ur => ur.RoleId);

WithMany中使用中间表

你可能感兴趣的:(efcore)