1

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.

2 Answers 2

1

It's about IEnumerable or IQueryable collection type. It should be List

0

Also you can edit these lines: This line: InsertedDate = table.Column(nullable: false), To be this: InsertedDate = table.Column(nullable: false, defaultValue:DateTime.Now),

And this line: Active = table.Column(nullable: false) To be this: Active = table.Column(nullable: false, defaultValue:true)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.