2021年4月27日星期二

Linq .Add() Function assigns 0 to added row

I'm using Linq for a project I'm working on, when I try to add a row though the added row's auto-incremented ID is 0.

The table it's being added to is set for auto increment.

I begin when the class is created by creating a new instance of the table the user is working with:

    public void AddingNewReference(Window sender)      {          Sender = sender;            SelectedReference = Context.DB.ACA_Reference.Add(new ACA_Reference());          SelectedReference.Active = true;      }  

The user fills in the information about the item and clicks the save button at the bottom of the window which runs:

    private void SaveAddedStandard()      {          if (string.IsNullOrEmpty(SelectedReference.Designation) || string.IsNullOrEmpty(SelectedReference.Description) || SelectedReference.Type == 0)          {              MessageBox.Show("Please complete all information before saving.");              return;          }            Context.DB.SaveChanges();          ResetWithoutReselectReferences ^= true;          Sender.Close();      }  

If I've tried to add more than one 'ACA_Reference' it says that the Primary Key is already in use.

Minimal, reproducible

     var context = new Entities();         var added = context.ACA_Reference.Add(new ACA_Reference());         var retrievedId = added.Id;   

I looked at the answers here: Programmatically Add Row to Linq-to-SQL Dataset and Jodrell said If you mean actually add the row to the back end storage and, assuming ID is an identity column and your model is correctly configured...

Has anyone else had this problem or know a solution? I've looked for a while online and don't want to have to try to grab the ID of the next row for obvious reasons. I might not have my model configured correctly but I can't figure out what I'm doing wrong.

Any help is appreciated, thanks in advance.

https://stackoverflow.com/questions/67292969/linq-add-function-assigns-0-to-added-row April 28, 2021 at 10:25AM

没有评论:

发表评论