mehdime / DbContextScope

A simple and flexible way to manage your Entity Framework DbContext instances
http://mehdi.me/ambient-dbcontext-in-ef6/
MIT License
634 stars 271 forks source link

Suggestions: how to retrieve ID generated by DB after saveChanges() #65

Open chaoscn opened 6 years ago

chaoscn commented 6 years ago

Hi,

We are using our database to generate ID instead from C# code, that means. The Id of a newly created object is only visible after dbContextScope.SaveChanges() is called. However, the mapping between EF models and domain models already happened in the repository layer which is before saveChanges() been called. This leads to a result that the domain object returned by the add method do not have the ID. How can I get around this issue?

Thanks, Chenhao Gao

Ruud2000 commented 5 years ago

I am struggling with the exact same issue as well. I noticed in the demo you assign a new Guid to the User Id within the UserCreationSpec which allows you to query based on this Id once created. However, if you let the database generate the ID how do I know which ID to use to query?

evanlarsen commented 5 years ago

I am having the same issue. Are we supposed to use the AmbientDbContextLocator to grab the dbcontext and then call savechanges from within the repository?

What implications does that have on the outer transaction?