Error saving to bank

Asked

Viewed 57 times

0

would like a help.

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

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

The primary keys are not auto-increment, I’m using a Function that generate the primary keys for me. My code 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.

  • The error says id is null. Probably the error is in the code you are using to generate the id. Do it like this: before saving, use an Alert out another similar code to write the id value on the screen, then you check if it is actually being created.

  • I already did that, the id value is not going null.

1 answer

2


By default the Entity Framework will understand that the primary key is being generated 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 waste:

Property(e => e.Id)
     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  • My friend, thank you so much, until the end I was able to record !!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.