EF Core无法确定抽象类中外键表示的关系 [英] EF Core unable to determine relationship represented by foreign keys in abstract class

查看:167
本文介绍了EF Core无法确定抽象类中外键表示的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为CRUD对象实现一个抽象类,其中包含对User类的两个引用;一个用于创建对象的用户,另一个用于最后修改对象的用户。实体框架无法确定由继承它们的类(部门)上的CreatedBy和ModifiedBy导航属性表示的关系。一个潜在的附加问题是User类还具有Department类的属性,该属性与Department上的CreatedBy和ModifiedBy属性无关。

I'm trying to implement an abstract class for CRUD objects which contains two references to the User class; one for the User which created the object and the other for the User which last modified it. Entity framework is unable to determine the relationship represented by the CreatedBy and ModifiedBy navigation properties on the class inheriting them (Department). A potential additional complication is that the User class also has a property of class Department, which is unrelated to the CreatedBy and ModifiedBy properties on Department.

错误消息如下:

无法确定导航属性部门表示的关系。类型为用户的CreatedBy。要么手动配置关系,要么使用 [NotMapped]属性或 OnModelCreating中的 EntityTypeBuilder.Ignore忽略此属性。

我正在使用dotnet 3.0和EF Core 3.0

I'm using dotnet 3.0 and EF Core 3.0

我尝试了ForeignKey数据属性的各种配置,并在EF文档之后使用了Fluent API,但我无法

I've attempted various configurations of the ForeignKey data attribute and using Fluent API following the EF documentation, but I was not able to get them to work.

public class User
{
    [Key]
    public int Id { get; set; }
    public Department Department { get; set; } = null!;
}

public class Department : AbstractCrudObject
{

}

public abstract class AbstractCrudObject
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("CreatedByUserId")]
    public User CreatedBy { get; set; } = null!;
    public int CreatedByUserId { get; set; }

    [ForeignKey("ModifiedByUserId")]
    public User ModifiedBy { get; set; } = null!;
    public int ModifiedByUserId { get; set; }
}


public class AppDbContext : DbContext
{
    public virtual DbSet<User> Users { get; set; } = null!;
    public virtual DbSet<Department> Departments { get; set; } = null!;
    public AppDbContext()
    {
    }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}


推荐答案


一个潜在的附加问题是User类还具有Department类的属性,该属性与Department上的CreatedBy和ModifiedBy属性无关。

A potential additional complication is that the User class also has a property of class Department, which is unrelated to the CreatedBy and ModifiedBy properties on Department.

这就是问题所在-EF Core不知道它们是否无关,还是与其中之一(以及哪一个)有关,因此您必须使用fluent API对其进行显式配置。

That's the problem - EF Core does not know whether they are unrelated, or it is related to one of them (and which one), so you have to configure that explicitly using the fluent API.

最简单的方法是指定所需关系的多重性和导航属性-在这种情况下,在依赖端具有3个多对一关系,并具有参考导航属性并且在主体末端没有集合导航属性:

The bare minimum is to specify the multiplicity and navigation properties of the desired relationships - in this case, 3 many-to-one relationships with reference navigation property at the dependent ends and no collection navigation property at the principal ends:

modelBuilder.Entity<User>()
    .HasOne(e => e.Department)
    .WithMany();

modelBuilder.Entity<Department>()
    .HasOne(e => e.CreatedBy)
    .WithMany();

modelBuilder.Entity<Department>()
    .HasOne(e => e.ModifiedBy)
    .WithMany();

这篇关于EF Core无法确定抽象类中外键表示的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆