Posts

Showing posts with the label Entity Framework

Entity Framework and TFS Build

I recently had an issue when trying to configure automatic deployments in TFS. I was building a MS Deploy package with one build definition and deploying it with another. The application was based on ASP.NET MVC with Entity Framework as a ORM When I tried to open the application on the target server, I got an error "Unable to load the specified metadata resource". The entity data model was configured to store the model files as resources into the assembly. The error was caused by the fact that the build process was no longer executing the packaging step. Usually Visual Studio does it automatically for you but MSBuild is not so kind. You have to manually tell it to execute the EntityDeploy step. I added an instruction in the project file (containing the data model) to execute the packaging step. < Target Name = " AfterBuild " > < EntityDeploy Sources = " Models\DataModel.edmx " outputPath = " . " > </ EntityDeploy ...

Get newly added entities in an Entity Framework query

A few days ago, a colleague of mine ran into a little problem using Entity Framework. He was adding new entities to the context and then wanted to display these entities. Unfortunately, since the new entities are only in memory, they are not fetched by the query. In fact, you have to use the ObjectStateManager accessible from the context to query in-memory entities. This object manages the entity cache in the context. You can use the following code to obtain every entity already in the cache. Assuming the type of the objects you want to query is Client : var listInMemory = context.ObjectStateManager .GetObjectStateEntries(EntityState.Added) .Select(e => e.Entity).OfType<Client>().ToList(); listInMemory will contain only the new added entities from the current context. You can combine EntityState values to retrieve entities in different states. You can also use an Union construct to add those entities to the list of entities returned by a standard Entity que...