When i insert a new entity in dbcontext EFCore can't populate id column with .AddRange() method

.net .net-core asp.net-core entity-framework-core

Question

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.

1
0
3/20/2020 3:25:41 PM

Accepted Answer

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

0
3/20/2020 5:14:58 PM

Popular Answer

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)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow