Return Object Code as soon as inserted

Asked

Viewed 2,456 times

10

I need to do an Insert in the Entity Framework and at the same time returns the code of the inserted object, thus preventing new access to the bank.

  • 1

    Hey, Marconi, check out my edition to try not to repeat the same mistakes. The more details you put in the question itself, the faster/easier it will be to get an answer. Good luck!

1 answer

12


I understand that you want the return of an object ID, if the ID column is integer and IDENTITY.

Whereas the mapping was done within the norms of the Entity Framework:

[DisplayColumn("Nome")]
public class Fruta
{
    [Key]
    public int FrutaId { get; set; }
    [Required]
    public String Nome { get; set; }
    ...
}

Just enter and read the Object ID:

var fruta = new Fruta { Nome = "Laranja" };
context.Frutas.Add(fruta);
context.SaveChanges();

var idDaFruta = fruta.FrutaId;
  • 2

    Gypsy thank you very much. I did not know that after saving the object I would already have his code available. I thank you for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.