EF core change _context.update to _context.add

Viewed 30

A table had a fine working _context.update(datarecord) in the Edit post controller.
However, it was decided that instead of updating it, every change should become a new entry.
So _context.add(datarecord)

But thats a problem, since EF is _context aware (you where updating Datarecord.keyxxx)
The key fields are auto generated EF does it for you.
And one may not write a key as keys are protected, which is still fine to me.
There hacks around that, just google :

  • Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF

But why go hacking, why alter the DB table enabling, when that future isn't recommended.
Better leave it to SQL/EF core to generate a new key entry for you.
But then how do I alter the _context to select a new entry. And be able to add or update that new entry instead?
(when i'm at the Edit controller)

1 Answers

solution is simple EF core accepts :

var mymodel is new MyTable
{
  // in here set all model fields except index keys


} 
_context.yourdb.Add(mymodel);
Related