简体   繁体   中英

EF Core unable to determine relationship represented by foreign keys in abstract class

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.

Error message is below:

Unable to determine the relationship represented by navigation property 'Department.CreatedBy' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

I'm using dotnet 3.0 and EF Core 3.0

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);
    }
}

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.

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM