Home > OS >  Entity Framework multi level inheritance using fluent
Entity Framework multi level inheritance using fluent

Time:06-20

I get an error when I want to create a multi-level table with fluent api for migration on Entity framework.

but i got fail: SQLite Error 19: 'FOREIGN KEY constraint failed'.

The table structures are as follows:

    Base Entity just for Id

        public class MainArt : BaseEntity
{

    public Paper Paper { get; set; }
    public int PaperId { get; set; }
}


    public class Paper : BaseEntity
{
    public string Name { get; set; }
    public string PaperPrice { get; set; }
    public ColorType ColorType { get; set; }
    public int ColorTypeId { get; set; }
}


    public class ColorType : BaseEntity
{
    public string Name { get; set; }
}

MainArt --> Paper --> ColorType

EF Fluent as below:

MainArt:

        public void Configure(EntityTypeBuilder<MainArt> builder)
    {

        builder.Property(p => p.Id).IsRequired();
        builder.HasOne(p => p.Paper).WithMany().HasForeignKey(p => p.PaperId);

    }

Paper:

        public void Configure(EntityTypeBuilder<Paper> builder)
    {

        builder.Property(p => p.Id).IsRequired();
        builder.Property(p => p.Name).IsRequired().HasMaxLength(100);
        builder.Property(p => p.PaperPrice).IsRequired();
        builder.HasOne(p => p.ColorType).WithMany().HasForeignKey(p => p.ColorTypeId);

    }
}

ColorType and Paper created with migration but MainArt failed.

I searched the web and i think iam missing something. ToTable or one-to-many i don't know.

Thanks.

CodePudding user response:

You maybe forgeting on Configure builder.HasKey(x=>x.Id); ?

CodePudding user response:

Thanks for answers.

i cant add exception as a comment because too long characters.

When asking a question, I removed some fields to make it a simple question. I just wanted to point it out to avoid confusion. Exception as below:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
  Failed executing DbCommand (4ms) [Parameters=[@p0='?', @p1='?', @p2='?', @p3='?', @p4='?', @p5='?'], CommandType='Text', CommandTimeout='30']
  INSERT INTO "MainArt" ("FrameId", "GlassId", "PaperId", "PaspartuId", "PhotoBlockId", "PrintingTypeId")
  VALUES (@p0, @p1, @p2, @p3, @p4, @p5);
  SELECT "Id"
  FROM "MainArt"
  WHERE changes() = 1 AND "rowid" = last_insert_rowid();

fail: Microsoft.EntityFrameworkCore.Update[10000]
  An exception occurred in the database while saving changes for context type 'Structure.Data.StoreContext'.
  Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
   ---> Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'FOREIGN KEY constraint failed'.
     at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)

thanks.

CodePudding user response:

I found out why it is giving exception. The code is not wrong.

Failure to create one of the child tables causing the error.

Migration and seeding worked correctly when I created the child table correctly.

Thanks.

  • Related