Blog: Entity Framework

Automatically migrate your Entity Framework Core managed database on ASP.NET Core application start

March 20, 2018, (updated on September 20, 2018), 2 comments, Software Development

If you use Entity Framework Core with migrations in your ASP.NET Core application and want to ensure that the database structure always matches the currently running application, you can simply migrate the database on application startup. This way you just have to redeploy your application and everything like migrating the data, change the schemas, etc. is done when the application is restarted. To do an automatic migration, register your DbContext implementation in the ASP.NET Core’s dependency injection system – the same way as it is already recommended by Microsoft. Because there is no active injection scope in the Configure() method, […] Read more...

Tags: , , ,

Getting started with Entity Framework Code First Migrations

October 12, 2015, (updated on March 1, 2019), Software Development

The following post describes the steps to setup and use Entity Framework Code First Migrations. For more information read this Microsoft page. The sample application shown in this article can be found on GitHub. Initial setup Listed below are the steps to setup a new project with an Entity Framework context, an entity class and an initial migration: Create a new data access project (C# library project) in your solution Open the Package Manager Console in the Visual Studio menu Tools > NuGet Package Manager > Package Manager Console. In the console window, select the previously created data access project […] Read more...

Tags: , , , , , , ,

EF & WCF: Serialize not loaded collection navigation properties as null rather than an empty collection

April 25, 2013, (updated on September 8, 2014), Software Development

Sometimes a web service client wants to know if a collection navigation property (a navigation property with “many” references) of a received entity object has been loaded on the server (e.g. by using Entity Framework’s Include method). By default, not loaded collection navigation properties are serialized as empty collections (not null). This is why the client cannot differentiate between a not loaded property and an empty collection. In one of my projects, I needed to have this information to know if a property is already loaded or has to be lazy loaded using an additional web service call. A way […] Read more...

Tags: , , , ,

Using Entity Framework Code-First proxies with WCF web services

April 30, 2012, (updated on December 21, 2014), 5 comments, Software Development

This blog post demonstrates how to set up a WCF web service together with lazy loading Entity Framework entities. The entities will be developed using a code first approach and their metadata is defined using the fluent API. To use all these features you need to install the newest Entity Framework (version 4.3.1 as of now) as a NuGet package (search for “Entity Framework“). In this blog we will work with a sample persistence model with two entity classes: persons and accounts. There is only one relation: Every person can have multiple accounts. The code of the two persistence classes […] Read more...

Tags: , , , ,

Retrieve Entity Framework DbContext from a proxy object

March 7, 2012, (updated on September 8, 2014), Software Development

There are some situations where you need to get the DbContext from a proxy object generated by the Entity Framework (current version: 4.5). Every generated proxy object holds a reference to the ObjectContext it belongs to. The problem is, that the ObjectContext does not have any information or reference to the corresponding DbContext. My solution is to maintain a static list with a ObjectContext-DbContext-Mapping: using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Objects; namespace Sample { public class MyDbContext : DbContext { private static readonly Dictionary<ObjectContext, MyDbContext> contexts = new Dictionary<ObjectContext, MyDbContext>(); public static MyDbContext FromObjectContext(ObjectContext context) { lock (contexts) { if (contexts.ContainsKey(context)) […] Read more...

Tags: , ,

Transactions in the Entity Framework

October 2, 2010, (updated on September 8, 2014), 2 comments, Software Development

First, I’ll show an example of the correct usage of an Entity Framework’s ObjectContext. At the beginning of the usage block, the ObjectContext is created; at the end it will be disposed (see using-Keyword). using (var ctx = new MyObjectContext()) { // do your stuff ctx.SaveChanges(); } Usually the code in the using block is not a whole transaction. The call of the method SaveChanges opens a new database connection, starts a new transaction, writes all changes and commits the transaction if no problems occurred. Every call of SaveChanges has its own transaction. If you want to call the SaveChanges […] Read more...

Tags: , , ,

  Page 1 of 1