I have two classes and I am trying to get Table-per-concrete-type mapping strategy to work for my inheritance. My setup has these two classes:
public abstract class DatabaseObject
{
public int Id { get; set; }
public bool IsVerwijderd { get; set; }
public DateTime? VerwijderdDatum { get; set; }
}
and
public class DefinitieBehandelaar : DatabaseObject
{
public string Naam { get; set; }
public string Code { get; set; }
}
My DbContext
has these settings for the OnModelCreating
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DatabaseObject>().UseTpcMappingStrategy();
}
In my migration file the code in the Up
function looks like:
migrationBuilder.CreateTable(
name: "DefinitieBehandelaars",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false),
IsVerwijderd = table.Column<bool>(type: "tinyint(1)", nullable: false),
VerwijderdDatum = table.Column<DateTime>(type: "datetime(6)", nullable: true),
Naam = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Code = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_DefinitieBehandelaars", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
and in my DataContextModelSnapshot
looks like:
modelBuilder.Entity("Database.DatabaseObject", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<bool>("IsVerwijderd")
.HasColumnType("tinyint(1)");
b.Property<DateTime?>("VerwijderdDatum")
.HasColumnType("datetime(6)");
b.HasKey("Id");
b.ToTable((string)null);
b.UseTpcMappingStrategy();
});
modelBuilder.Entity("Backend.Database.Definitie.DefinitieBehandelaar", b =>
{
b.HasBaseType("Database.DatabaseObject");
b.Property<string>("Code")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Naam")
.IsRequired()
.HasColumnType("longtext");
b.ToTable("DefinitieBehandelaars");
});
However, when I try to add DefinitieBehandelaar
s via the DbContext
like so:
dbContext.DefinitieBehandelaars.Add(new DefinitieBehandelaar { Naam = "Me", Code = "1"});
dbContext.SaveChanges();
I keep receiving the error:
MySqlException: Field 'Id' doesn't have a default value
My project is on .NET 8.0.
I think I understand the error: for some reason, the Id
from DatabaseObject
is not being created as an AUTO_INCREMENT
(see MySQL definition below), however I do not understand that because Id
is marked as ValueGeneratedOnAdd
in the DataContextModelSnapshot
. Am I not doing TPC correctly, or what is going on here?
CREATE TABLE `definitiebehandelaars` (
`Id` int NOT NULL,
`IsVerwijderd` tinyint(1) NOT NULL,
`VerwijderdDatum` datetime(6) DEFAULT NULL,
`Naam` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL,
`Code` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci
LADADOOS L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.