简体   繁体   中英

Entity Framework Core: multiple relationships to one table of base type

Let's assume that Administrator , Purchaser and Supplier have User base type and remaining models look following:

public class Vendor
{
    public int VendorId { get; set; }
    public List<Supplier> Suppliers { get; set; }
}
public class Task
{
    public int TaskId { get; set; }
    public Administrator Admin { get; set; }
    public List<Purchaser> Purchasers { get; set; }
    public Vendor Vendor { get; set; }
}

Now I would like to create a UserTask table that contains IDs of all users of the Task : an Admin , Purchasers and Suppliers of the Vendor in column User and their Tasks IDs in column Task .

How could I configure such setup in Fluent API?

Edit:

I created additional entity UserTask that consists of IDs and navigation properties:

public class UserTask
{
    public int UserId { get; set; }
    public User User { get; set; }

    public int TaskId { get; set; }
    public Task Task { get; set; }

    //some other needed properties
}

And tried to configure models like this:

modelBuilder.Entity<UserTask>(ut =>
{
   ut.HasKey(x => new { x.UserId, x.TaskId });
   ut.HasOne(u => u.User).WithMany()
      .HasForeignKey(u => u.UserId)
      .OnDelete(DeleteBehavior.Cascade);
   ut.HasOne(t => t.Task).WithMany()
      .HasForeignKey(t => t.TaskId)
      .OnDelete(DeleteBehavior.Cascade);
});

modelBuilder.Entity<Task>(t =>
{
   t.HasMany(p => p.Purchasers).WithMany(p => p.Tasks);
   t.HasOne(a => a.Administrator).WithMany(); //adding a => a.Task expression in parameter throws error that the relationship is already defined
   t.HasMany(s => s.Vendors.Suppliers).WithMany(s => s.Tasks); //throws error
});

And it fails because HasMany(s => s.Vendors.Suppliers) i not a valid member access expression. Is there any way to overcome this issue?

Considering the relationships in these tables, add a property so that Fluent API can reference the relationship. About the specific modelbuilder.

        modelBuilder.Entity<Supplier>()
            .HasOne(x => x.vendor)
            .WithMany(y => y.Suppliers);
        modelBuilder.Entity<Administrator>()
            .HasOne(a => a.tasks)
            .WithOne(t => t.Admin)
            .HasForeignKey<Administrator>(f=>f.AdministratorId);
        modelBuilder.Entity<Vendor>()
            .HasOne(a => a.tasks)
            .WithOne(t => t.Vendor)
            .HasForeignKey<Vendor>(f=>f.VendorId);

The model need to be redesigned as this.

public class User
{
    public int id { get; set; }
    public string Property { get; set; }
}
public class Vendor
{
    public int VendorId { get; set; }
    public List<Supplier> Suppliers { get; set; }
    public Tasks tasks { get; set; }
}
public class Tasks
{
    [Key]
    public int TaskId { get; set; }
    public Administrator Admin { get; set; }
    public List<Purchaser> Purchasers { get; set; }
    public Vendor Vendor { get; set; }
}

public class Supplier:User
{
    public int SupplierId { get; set; }
    public string SupplierProperty { get; set; }
    public Vendor vendor { get; set; }
}
public class Administrator:User
{
    public int AdministratorId { get; set; }
    public string adminProperty { get; set; }
    public Tasks tasks { get; set; }
}
public class Purchaser:User
{
    public int PurchaserId { get; set; }
    public string purProperty { get; set; }
    public Tasks tasks { get; set; }
}

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