July 8, 2014


Using Entity Framework API for .Net Developer Platform application

In this blog, I am going to explain how the Entity Framework Fluent API can be used for creating a platform application for .Net Developers which has to support ORM features as well. This platform application has to provide entity management privilege to the client application, which means that the client application can create & manage its entities in database via the platform application.

But! Before we start, we have to understand what the general expectations from the platform application are when it has to support development as well as entity management. Also have to understand how the Entity Framework Fluent API can be used in such an application.

So let’s have a look at the key features that are desired in our platform application –

  • Scalability
  • Entity Customization
  • Code Encapsulation

Let’s understand how Entity Framework Fluent API will support the desired behavior.

But first all let’s remember how we generally use Entity framework Fluent API Code first approach in our general application. We create a context class that inherits DbContext, let’s name this as platform context class then declare entities DbSet in the class and the rest of the entity configuration is provided in the OnModelCreating

Check link for details http://www.entityframeworktutorial.net/code-first/database-initialization-in-code-first.aspx

In our platform application we will follow the same approach as explained above to facilitate entity management.

Now the next question that comes to your mind is “how our client application is going to consume this platform for his/her entity creation”.

To achieve this you have to create a client context class that inherit platform context class. (This is how we can achieve maximum code encapsulation, and here client can’t change any platform entities related behavior.)

In client context class, the client entities Dbset will be declared. (This is how will support scalability in our application, client is allowed to add n number of entities.)

In client context class, we will override OnModelCreating, using which we will have full control over our client entities (and in turn support entity customization.)

One of the most important feature that we have to support while using Entity Framework Fluent API is DbMigration else our  client application will through exception “The model backing the context has changed since the database was created”.

So to implement DbMigration you have to create a sealed configuration class in the client application that inherits DbMigrationsConfiguration and set AutomaticMigrationsEnabled and AutomaticMigrationDataLossAllowed to true.

This DbMigration has to set in the startup of the client application using command Database.SetInitializer

Check link for details http://msdn.microsoft.com/en-in/data/jj591621.aspx

Now our platform application and its client application are ready to be consumed for any entity creation.

References –