FrankO
FrankO

Reputation: 2562

Entity Framework Saving new Objects Returns a Populated ID?

Please help me understand this.

When I save a new object to the database (using Entity Framework), am I able to access the newly created ID (Identity column) value in the line(s) after I call db.SaveChanges()?

public Class TheObject
{
    public int ID { get; set; }
    public string FirstName { get; set; }
}

.....

TheObject MyObject = new TheObject();
MyObject.FirstName = "John";

db.TheObjects.Add(MyObject);
db.SaveChanges();

int MyNewID = MyObject.ID; //Am I able to access MyObject.ID here and have access to the value that was just created in the database?

Upvotes: 0

Views: 60

Answers (1)

Sean Barlow
Sean Barlow

Reputation: 588

Yes you are but why not just run that code and test it yourself?

Upvotes: 1

Related Questions