5

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.

Ricky
  • 10,044
  • 3
  • 26
  • 31
netik
  • 1,736
  • 4
  • 22
  • 45

2 Answers2

4

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.)

Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
  • 2
    So what is the replacement of `SetInitializer`? This answer doesn't actually seem to answer the question for me. I'm still not sure how to get the equivalent of `DropCreateDatabaseIfModelChanges` – Reafidy Jul 04 '15 at 05:13
4

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

Jonas Lundgren
  • 1,031
  • 11
  • 17
  • I am indeed using database migrations. One of the common ways to apply these migrations from code is using the `Database.SetInitializer` method. Are you suggesting that the CLI is the only way to do that from Core? – MEMark Oct 26 '16 at 07:49
  • @MEMark you can call migrations from code, see http://stackoverflow.com/questions/37780136/asp-core-migrate-ef-core-sql-db-on-startup – Jonas Lundgren Oct 28 '16 at 07:35
  • Thx. So in short, just use `dbContext.Database.Migrate();`. – MEMark Oct 31 '16 at 14:47