ASP.NET vNext MVC and Entity Framework issues

asp.net-core entity-framework entity-framework-core

Question

I'm trying to create a little ASP.NET vNext WebAPI + AngularJS + Entity Framework project. But obviously, a lot changed in EF7 so I am experiencing the following issues:

I changed the project.json as following:

"dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta1",
    "EntityFramework": "7.0.0-beta1",
    "EntityFramework.SqlServer": "7.0.0-beta1",
    "EntityFramework.Commands": "7.0.0-beta1",
    "Microsoft.AspNet.Mvc": "6.0.0-beta1",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta1",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta1"

In my DataContext class, I am trying the following:

using System;
using Project1.Models;
using Microsoft.Data.Entity;

namespace Project1.DataAccess
{
    public class DataContext
    {
        public DbSet<Website> Websites { get; set; }

        public DataContext()
        {
            Microsoft.Data.Entity.Infrastructure.Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataContext>());
        //Database.SetInitializer(new DropCreateDatabaseAlways<DataContext>());
        }

     } 
}

}

First of all: Why has the namespace System.Data.Entity changed to Microsoft.Data.Entity ? I cannot find anything about this change in any microsoft msdn article!!

Second: The whole Database.SetInitializer doesn't work anymore. It recommends to use the Microsoft.Data.Entity.Infrastructure namespace but that Database class doesn't contain a SetInitializer method.

Accepted Answer

Because EF7 is still in pre-release, you won't find any documentation on it in MSDN articles yet; you'll need to look at the EF7 GitHub Wiki for any information.

As you try out EF7 please bear in mind that this is a very early stage in the development of the new EF codebase and there are many features that are partially implemented or not yet available.

I believe that SetInitializer is one of those things that has been implemented completely differently; the team is avoiding static methods in order to improve testability of the framework.

Also note that the latest version of EF7 is 7.0.0-beta3, but the wiki provides information on how to use the nightly builds. (Using nightly builds may be rough given the severe changes since VS2015 CTP6 was released.)


Popular Answer

EF7 is more lightweight and modular to support new platforms and non-relational data stores. The changes are quite fundamental so some underlying APIs has changed, including their namespaces.

The new lightweight nature means that less things will happen automatically behind the scenes compared to previous versions. Database initializers have been removed so databases won’t be created on demand. Instead, you are supposed to control this process yourself using Database Migrations. For more info: ASP.NET vNext (MVC6) Ground Up #3 - Entity Framework 7



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why