欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-03 10:31:42
当子表的多个外键关连同一主表时,EF Core模型应该如何配置呢? 例:记录一条销售单信息,需要保存销售员、和制单人(记录的操作人),而这两个字段都需要指向User表。 销售单表(子表)B01_SO模型类如下: using System; using System.Collections.Gener ......

 当子表的多个外键关连同一主表时,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();
            
        }        
    }
}