So, consider the following class:
public class Solution
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
And the following methods
public void DoSomething()
{
new Item originalItem = new Item();
item.Name = "Test";
InserIntoDb(originalItem);
Assert.True(item.Id != 0);
}
public void InserIntoDb(Item item)
{
context.Item.Add(item);
context.SaveChanges();
}
On this case, after insert into the db, EF automatically updates the originalItem with its auto generated ID value.
My problem starts when I add some logic to only add to the db if the Name doesn't exist. If exists the Insert doesn't happen therefore the ID property is not populated.
My question is: Is there a way to make the context.Item (when retrieving all) to automatically update the ID of the originalItem value without having to change the InsertIntoDb method to return an Item and consequently having to add a line like
originalItem = InsertIntoDb(originalItem);
Thanks, in advance