Unfortunately there is no SavingChanges event on the code first DbContext, but there is a way to get to it. The DbContext used for data access with Entity Framework Code First has a simplified API compared to the ObjectContext which is good. Unfortunately it is a bit too simple sometimes. For example there is no… Continue reading EF Code First DbContext.SavingChanges
Category: C#
Lambda Closures Handle Event Handlers’ State
Subscribing to an event with a method that requires state is a pain because it often requires a class to be created to hold that state. Fortunately we can use a lambda expression to have the compiler create that state for us instead. It is not an uncommon scenario to have an event handler that… Continue reading Lambda Closures Handle Event Handlers’ State
[Flags] on Enums Make ToString() Smart
Putting the [Flags] attribute on an enum changes ToStrings behaviour. It generates a comma separated list. I’ve used enums a lot. I’ve occasionally used the [Flags] attribute too. But I’ve never paid attention to how ToString() changes behaviour when the [Flags] attribute is added. Not until a couple of days ago, when I learnt something… Continue reading [Flags] on Enums Make ToString() Smart
Make the DbContext Ambient with UnitOfWorkScope
The Entity Framework DbContext (or LINQ-to-SQL DataContext) is a Unit Of Work implementation. That means that the same DbContext should be used for all operations (both reading and writing) within a single web or service request. That means that there are a lot of different places where the DbContext have to be accessed. To avoid… Continue reading Make the DbContext Ambient with UnitOfWorkScope
Dissecting MVC Scaffolded Code for EF Updates
MVC and Entity Framework scaffolding is powerful, but I prefer to know what actually happens before accepting generated code. The code generated to update an entity when an edit has been done looked interesting, so I decided to have a closer look at it. When an edit is done to an entity, the data is… Continue reading Dissecting MVC Scaffolded Code for EF Updates