Index was out of range. after insert EF Core

Viewed 316

I am using EF Core to handle my database and I want insert a record to my postgresql database.

But when I insert the record and do the save changes, the app throws an error:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

I understand that this error can appear when I work with collections but in this case I am doing an insert.

my base entity:

public abstract class BaseEntity: IBaseEntity<long>
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key, Column("id", Order = 0)]
    public long Id { get; set; }

    [Column("createdAt")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreatedAt{ get; set; }

}

the insert from business layer:

  var entityToInsert = new Delivery()
  { 
       State = 1, //active
       Date = DateTime.Now,
  };

  await this.UOW.Delivery.CreateAsync(entityToInsert);
  await this.UOW.SaveChangesAsync();

My generic repository method:

    public async Task<TEntity> CreateAsync(TEntity entity)
    {
        await Context.AddAsync<TEntity>(entity);
        return entity;
    }

the delivery table definition:

id         bigint // type identity "by default" autoincrment 1 per 1
state      integer 
date       timestamp without time zone  (not null)
createdAt  timestamp without time zone  (not null)

What am I doing wrong?

0 Answers
Related