Automatically update ID value for records retrieved

c# entity-framework entity-framework-6

Question

So, consider the following class:

public class Solution
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
}

and the subsequent techniques

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();
}

In this instance, EF immediately changes the originalItem with the auto-generated ID value after inserting the data into the database.

My issue arises when I add logic to only add to the database if the Name is empty. The ID property is empty since the Insert does not take place if the condition is true.

I want to know if there is a way to avoid changing the InsertIntoDb function to return an Item and having to add a line like this: Is there a way to have the context.Item (when retrieving all) automatically update the ID of the originalItem value?

originalItem = InsertIntoDb(originalItem);

Thank you in advance.

1
1
9/21/2018 6:58:51 PM

Popular Answer

ZZZ_tmp
0
9/21/2018 6:58:31 PM


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