EF Core子表多个外键关连同一主表设置方法

 当子表的多个外键关连同一主表时,EF Core模型应该如何配置呢?

例:记录一条销售单信息,需要保存销售员、和制单人(记录的操作人),而这两个字段都需要指向User表。

销售单表(子表)B01_SO模型类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace Test.Models
{
    public class B01_SO
    {
       [Key]
        public int ID { get; set; }

        [Display(Name ="销售单号")]
        [StringLength(50)]
        [Required]
        public string SONum { get; set; }

        [Display(Name = "单据编号")]
        [StringLength(50)]        
        public string TKNum { get; set; }


        [Display(Name = "交期")]
        [Required]
        public DateTime Dtime { get; set; }

        [Display(Name = "产品描述")]
        [StringLength(200)]
        [Required]
        public string Description { get; set; }


        [Display(Name = "销售员")]
        public int? SaleID { get; set; }
        [Display(Name = "销售员")]
        public User Sales { get; set; }


        [Display(Name = "制单人")]        
        public int? UserID { get; set; }
        [Display(Name = "制单人")]
        public User User { get; set; }

    }
}

User表(主表)模型类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Test.Models
{
    public class User
    {
        [Key]
        public int ID { get; set; }

        [Display(Name = "用户名")]
        [StringLength(50)]
        [Required]
        public string Name { get; set; }

        [Display(Name = "邮箱")]
        [StringLength(100)]
        [Required]
        public string Email { get; set; }

        [Display(Name = "密码")]
        [StringLength(50)]
        [Required]
        public string Password { get; set; }

        [Display(Name = "是否启用")]
        [Required]
        public bool Enabled { get; set; }

        [Display(Name = "性别")]
        [StringLength(10)]
        [Required]
        public string Gender { get; set; }

        [Display(Name = "中文名")]
        [StringLength(100)]
        public string ChineseName { get; set; }

        [Display(Name = "英文名")]
        [StringLength(100)]
        public string EnglishName { get; set; }

        [Display(Name = "照片")]
        [StringLength(200)]
        public string Photo { get; set; }

        [Display(Name = "QQ")]
        [StringLength(50)]
        public string QQ { get; set; }

        [Display(Name = "公司邮箱")]
        [StringLength(100)]
        public string CompanyEmail { get; set; }

        [Display(Name = "工作电话")]
        [StringLength(50)]
        public string OfficePhone { get; set; }

        [Display(Name = "分机号")]
        [StringLength(50)]
        public string OfficePhoneExt { get; set; }

        [Display(Name = "家庭电话")]
        [StringLength(50)]
        public string HomePhone { get; set; }

        [Display(Name = "手机号")]
        [StringLength(50)]
        public string CellPhone { get; set; }

        [Display(Name = "地址")]
        [StringLength(500)]
        public string Address { get; set; }

        [Display(Name = "备注")]
        [StringLength(500)]
        public string Remark { get; set; }

        [Display(Name = "身份证")]
        [StringLength(50)]
        public string IdentityCard { get; set; }


        [Display(Name = "生日")]
        public DateTime? Birthday { get; set; }
        [Display(Name = "任职时间")]
        public DateTime? TakeOfficeTime { get; set; }
        [Display(Name = "上次登录时间")]
        public DateTime? LastLoginTime { get; set; }
        [Display(Name = "创建时间")]
        public DateTime? CreateTime { get; set; }
        
        public ICollection<B01_SO> SalseB01_SOs { get; set; }
        public ICollection<B01_SO> UserB01_SOs { get; set; }

    }

    
}

Fluent  API 配置:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace TestCore.Models
{
    public class TestCoreContext : DbContext
    {
        #region 启用控制台日志 EF Core
        public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseLoggerFactory(MyLoggerFactory);
        #endregion

        public TestCoreContext(DbContextOptions<TestCoreContext> options) : base(options)
        {
        }
        
        public DbSet<User> Users { get; set; }        
        public DbSet<B01_SO> B01_SO { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<B01_SO>()
                .HasOne(u => u.User)
                .WithMany(u => u.UserB01_SOs)
                .HasForeignKey(s => s.UserID)
                .OnDelete(DeleteBehavior.Restrict)
                .IsRequired();
            modelBuilder.Entity<B01_SO>()
                .HasOne(u => u.Sales)
                .WithMany(u => u.SalseB01_SOs)
                .HasForeignKey(s => s.SaleID)
                .OnDelete(DeleteBehavior.Restrict)
                .IsRequired();
            
        }        
    }
}

 

posted @ 2021-03-25 21:03  谁的谁是谁  阅读(232)  评论(0编辑  收藏  举报