EFCore can't populate identity column after insert.How can i provide automatically populate this property after insert with .AddRange() method ? By the way I'm using MySql database.
My AddRange method in repo:
public void AddRange(IQueryable<TEntity> entities)
{
Entities.AddRange(entities);
SaveChanges();
}
My BaseEntity class :
/// <summary>
/// All entities have this values.
/// </summary>
public abstract class BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime InsertedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
public DateTime? DeletedDate { get; set; }
public bool Active { get; set; }
}
Migration of one of my entities:
migrationBuilder.CreateTable(
name: "HappyHour",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
InsertedDate = table.Column<DateTime>(nullable: false),
UpdatedDate = table.Column<DateTime>(nullable: true),
DeletedDate = table.Column<DateTime>(nullable: true),
Active = table.Column<bool>(nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_HappyHour", x => x.Id);
});
Modelsnapshot of my migration :
modelBuilder.Entity("Entity.HappyHour", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<bool>("Active")
.HasColumnType("tinyint(1)");
b.Property<DateTime?>("DeletedDate")
.HasColumnType("datetime(6)");
b.Property<DateTime>("InsertedDate")
.HasColumnType("datetime(6)");
b.Property<DateTime?>("UpdatedDate")
.HasColumnType("datetime(6)");
b.HasKey("Id");
b.ToTable("HappyHour");
});
Versions of Nuget Packages :
Microsoft.EntityFrameworkCore : 3.1.1,
MySql.Data.EntityFrameworkCore : 8.0.19,
Pomelo.EntityFrameworkCore.MySql : 3.1.0
This is my first question. Thank you for helping.