EF Core 8 , tph inheritance, one to many relationship and automatic foreign key

I’m developing a web site application where clients may upload files. Some of the files may be attachments for some documents, and some are not. I’m using SQL Server as the database.

I decided to go with the following scheme: one table for storing information about uploaded files with parent id and parenttype for relationship one (doc) to many (files).

I don’t need automatic navigation from file to parent document.

This is my code:

public class FilesFromClients
{
    public int id { get; set; } //primary key 

    // some other properties
    public int ParentId { get; set; }
    public ParentType pType { get; set; }   // discriminator
}

public enum ParentType : int { NOTSET = 0, SOME=1, HRMAGREEMENT=2 };

File may have parent (and parenttype) (so it is an attachment to some document) or may be unrelated to any parent, so parentid is 0 and pType = ParentType.NotSet (or 0).

Therefore, I can’t use these properties for unique key

public class HrmFiles : FilesFromClients {}  //for discriminator lately

// plan to add more type of docs here 

public class NewHrmAgreement 
{
    public int Id { get; set; } // primary key 

    // other properties
    ICollection<HrmFiles> Files { get; set; }
}

public virtual DbSet<FileFromClients> tblFiles;
public virtual DbSet<HrmFiles> hrmFiles;

// OnModelCreating 
modelBuilder.Entity<FileFromClient>(e =>
  {
      e.HasDiscriminator<ParentType>(c => c.PType)
           .HasValue<cFileFromClient>(ParentType.NOTSET)
           .HasValue<cUploadsNoParents>(ParentType.SOME)
           .HasValue<HrmFiles>(ParentType.HRMAGREEMENT);    
  });

modelBuilder.Entity<NewHrmAgreement>(e =>
   {
      e.HasMany(a => a.Files)
       .WithOne()
       .HasForeignKey(f => f.ParentId);
   });

Migration adds an additional foreign key

migrationBuilder.AddForeignKey(
    name: "FK_FilesFromClients_NewHrmAgreements_ParentId",
    table: "FilesFromClients",
    column: "ParentId",
    principalTable: "NewHrmAgreements",
    principalColumn: "ID");      

As far as I plan to add more docs (and discriminator values), I think that this FK is unnecessary or wrong.

If I am trying to update the database, I’m getting

Introducing FOREIGN KEY constraint ‘FK_FilesFromClients_NewHrmAgreements_ParentId’ on table ‘FilesFromClients’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.

I tried different values in OnDelete()

e.HasMany(a => a.Files) 
 .WithOne()
 .HasForeignKey(f => f.ParentId)
 .OnDelete(DeleteBehavior.ClientNoAction);

but just getting different errors when tried to update database
like

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint “FK_FilesFromClients_NewHrmAgreements_ParentId”. The conflict occurred in database “autoabcNGTest”, table “dbo.NewHrmAgreements”, column ‘ID’

If I manually remove this FK from the migration, everything seems to work as expected. EF Core adds parenttype to queries and creates correct records in db and deletes them.

Now I’m choosing between either remove mapping for NewHrmAgreement.Files and do all related staff manually, or just manually removing the FK from migration.

Am I missing something or doing something wrong?

Thanks in advance.

ish

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật