最近从.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