DEV Community

Discussion on: Use Action Filters to cut down your context.SaveChanges calls

Collapse
 
leolima32 profile image
Leonardo Ferreira

Great article! Really nice approach. What can I do in this scenario if I want to return the generated id from the insertion?

Collapse
 
jonhilt profile image
Jon Hilton

Thanks Leonardo,

As to your question. Yeah that's a little tricky.

I've tended towards using GUIDs recently for this exact reason. I don't like the idea of having to round-trip to the database to get an id for an entity.

Apart from anything it makes testing harder because the database ends up being involved in something which could otherwise be tested entirely by spinning up instances of entities in memory and exercising them without ever hitting the database.

I guess a pragmatic solution is to still call SaveChanges where absolutely necessary (to generate an id for example) but try to engineer away from it where you can :-)