EFcore两个实体两个一对多关系实现问题探讨

最近从.NET Framework 转写一个EFCore,原来的实体模型不能用,用Code First发现这个问题,

存在两个实体

AppuUser类

public class AppUser
    {

        [Key]
        [Display(Name = "用户编号")]
        public int UserId { get; set; }

        [Display(Name = "用户组")]
        public string UserTeam { get; set; }

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

        [Display(Name = "用户编号")]
        public string UserNameCharactor { get; set; }

        [Display(Name = "电话")]
        public string UserTel { get; set; }


        public virtual UserRole Role { get; set; }
   
        public virtual ICollection<Account> OwnAccounts { get; set; }

        public virtual ICollection<Account> CheckAccounts { get; set; }


        public virtual ICollection<Bill> Bills { get; set; }

    } 

Account类

    public class Account
    {
        [Key]
        public int AccountId { get; set; }

        public int OwnnerId { get; set; }
        public virtual AppUser Owner { get; set; }

        public float CurrentAccount { get; set; }

        public DateTime CheckDate { get; set; }
        public AccountCheckSource CheckSource { get; set; }

        public int CheckerId { get; set; }
        public virtual AppUser Checker { get; set; }

        public virtual Bill Bill { get; set; }



    }

两个实体存在两队一对多关系,即一个用户拥有多个账户,一个用户可以Check多个账户,在Account内增加两个标量外键和导航属性,在Appusr内增加两个Icollection导航属性。

以上,执行Add Migration时发现无法确定Account.Appuser Owner的外键关系。报错:Unable to determine the relationship represented by navigation property 'Account.Owner' of type 'AppUser'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

那么如何打FK标签属性才能正确让EFCore确认关系呢

经过官方文档的查看和学习,有两种方式

1.Fluent Api

在DbContext中重写OnModelCreating方法。

 protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<AppUser>().HasMany<Account>(a=>a.OwnAccounts).WithOne(a => a.Owner).HasForeignKey(a => a.OwnnerId);
            builder.Entity<AppUser>().HasMany<Account>(a=>a.CheckAccounts).WithOne(a => a.Checker).HasForeignKey(a => a.CheckerId);
        }

2.采用Attribute注解方式

    public class AppUser
    {

        [Key]
        [Display(Name = "用户编号")]
        public int UserId { get; set; }

        [Display(Name = "用户组")]
        public string UserTeam { get; set; }

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

        [Display(Name = "用户编号")]
        public string UserNameCharactor { get; set; }

        [Display(Name = "电话")]
        public string UserTel { get; set; }


        public virtual UserRole Role { get; set; }

        [InverseProperty("Owner")]
        public virtual ICollection<Account> OwnAccounts { get; set; }

        [InverseProperty("Checker")]
        public virtual ICollection<Account> CheckAccounts { get; set; }


        public virtual ICollection<Bill> Bills { get; set; }

    } 

    public class Account
    {
        [Key]
        public int AccountId { get; set; }

        public int OwnnerId { get; set; }
        public virtual AppUser Owner { get; set; }

        public float CurrentAccount { get; set; }

        public DateTime CheckDate { get; set; }
        public AccountCheckSource CheckSource { get; set; }

        public int CheckerId { get; set; }
        public virtual AppUser Checker { get; set; }

        public virtual Bill Bill { get; set; }



    }

废话不多说,上官方文档的相关页面链接:https://docs.microsoft.com/zh-cn/ef/core/modeling/relationships?tabs=data-annotations%2Cfluent-api-simple-key%2Csimple-key

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值