Search code examples
asp.net-mvcentity-frameworkproduction-environment

Entity Framework 5 - Code First Production Preparation


As part of our intended migration from Linq2Sql to Entity Framework, we have created a small EF model consisting of ten entities. Because we are working with a legacy application we are using the 'code first' option and manually mapping our POCO objects to tables within the database. We do not want the Entity Framework code to update the database. I believe that the following is all I need to do to achieve this - I would appreciate confirmation as I was not sure whether the SetInitializer code should go here or perhaps in the Global.asax code.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Database.SetInitializer<InboxDataContext>(null);

    modelBuilder.Configurations.Add(new Subscribers.Mapping.DataSourceMap());
    modelBuilder.Configurations.Add(new Subscribers.Mapping.SubscriberMap());
    ...
}

As part of this update, we have created a small test page (MVC controller and views) and added it to our main application project. During development and testing this has worked well. The only thing we did notice is that if we updated the underlying EF model, the initial page load seemed to take several minutes to render. I assumed this was because EF had to rebuild its views and mappings to reflect the changes - to my disgrace, I didn't worry too much about this as a) I don't have a full understanding of how this works and b) it only seemed happened on model changes.

This morning we loaded this update to the production server for initial testing and this time the initial load problem became much worse - so much so in fact that the test page never loaded.

So, to get to my questions:

  1. Is there anything else I should/should not be doing to prepare my application for production?
  2. How can I improve the initial load performance?
  3. How can I find out why the initial load on the production server never completes - I am not seeing any error messages?

I did try downloading the Entity Framework Power Tools and running the 'Generate Views' option on the derived DbContext class, but since I could only run this in the development environment I was not sure whether this would work when copied to the production server - it didn't appear to.

I should mention that the development environment contains just one hundred records in the main table and the production environment contains almost one million records (same schemas), again I wasn't sure if this would make any difference.

Any help advice would be appreciated.

Thanks.


Solution

  • Firstly, if you are using an existing database then a better approach would be Model First. You can connect your Edmx to your current database and it will gather all the required mappings as they currently stand. It then also generates the POCO classes and DbContext (with EF5, you have to do it manually in <5). Using this approach may cut down on initialisation time as your code wont have to create a model at runtime.

    Give this a try, and it might solve all your problems (I believe they are all related).