Error saving to bank

0

I would like some help.

When I try to save the registry, the following message appears:

  

23502: null value in column "id" violates not-null constraint

The primary keys are not autoincrement, I'm using a function that generate the primary keys for me. My code that I'm doing to save:

 public async Task Salvar(T obj)
    {
        Db.Set<T>().Add(obj);
        await Db.SaveChangesAsync();//TA DANDO ERRO AQUI
    }

My id is already going with a value.

    
asked by anonymous 30.08.2017 / 16:27

1 answer

2

By default the Entity Framework will understand that the primary key is being generated by DatabaseGerated and it will ignore the value you manually enter as key, you have to specify that it will not be:

[DatabaseGenerated(DatabaseGeneratedOption.None)]

or in the case of fluent:

Property(e => e.Id)
     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
30.08.2017 / 16:42