Entity framework core - user specified primary key value not saving

entity-framework-core primary-key

Question

I am using this code to save a student to student table. ID is the primary key in the table.

_context.Add(student);
await _context.SaveChangesAsync();

A student object with these values works and the record is added with the next available ID in the table.

Firstname: Firstname
Lastname: Lastname
Date: {03/02/2019 23:59:00}
ID: 0

The below student object fails and causes a "DbUpdateException" Exception.

Firstname: Firstname
Lastname: Lastname
Date: {03/02/2019 23:59:00}
ID: 66 (or any other int value that isn't already used in the table)

Is it not possible to specify a value for a Primary Key in EF? Or is there a particular way of specifying a user value?

1
0
2/26/2019 9:13:03 AM

Popular Answer

Maybe you are using auto-generated primary key?

0
2/26/2019 10:00:32 AM


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