Octavian Epure
Octavian Epure

Reputation: 1039

EF 7 Code first DB creation issue

I followed a tutorial on pluralsight for ASP .NET 5 and EF 7 using Code First DB. This is the connection string :

"WorldContextConnection": "Server=CVU-OCTAVIANE\\SQLEXPRESS;Database=TheWorldDB;Trusted_Connection=true;"

I added the initial migration (which worked fine) and then I tried to use a EF 7 feature to create the DB automatically: I created a Context type class where I used Database.EnsureCreated() to create the DB on the first run. This is the context class:

public class WorldContext : DbContext
{
    public WorldContext()
    {
        Database.EnsureCreated();
    }

    public DbSet<Trip> Trips { get; set; }
    public DbSet<Stop> Stops { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connString = Startup.Configuration["Data:WorldContextConnection"];

        optionsBuilder.UseSqlServer(connString);
        base.OnConfiguring(optionsBuilder);
    }
}

When I ran the project "A severe error occurred on the current command"

Thanks

Upvotes: 1

Views: 315

Answers (2)

Elek Guidolin
Elek Guidolin

Reputation: 527

Actually I had the same problem of yours, and I took a few steps less.

  • I removed the Migration folder
  • I updated my global.json to the default version of sdk that I'm using, (check using "dnvm list") which in my case was "update2".
  • Deleted the TheWorlDb from my SQL instance.
  • After that, I just generated again the migration of InitialDatabase
  • Clear, Build and Rebuild the solution.

Upvotes: 0

Priceline Negotiator
Priceline Negotiator

Reputation: 410

I had the same problem and seem to have resolved it.

I'm not sure if all of the below is required, but are the steps I took

  • Deleted all migration .cs files under the Migrations folder
  • Deleted TheWorldDB from SQL Server Object Explorer
  • Closed Visual Studio
  • Upgraded ASP.NET 5 to RC1 Final at get.asp.net
  • Opened Visual Studio
  • Updated global.json to use "version": "1.0.0-rc1-final"
  • Updated project.json to use latest "dependencies"
    • "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    • "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    • "Microsoft.AspNet.Mvc.Abstractions": "6.0.0-rc1-final",
    • "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    • "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    • "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    • "Microsoft.Extensions.Configuration": "1.0.0-rc1-final",
    • "EntityFramework.Core": "7.0.0-rc1-final",
    • "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    • "EntityFramework.Commands": "7.0.0-rc1-final"
  • Ran dnvm install to get "1.0.0-rc1-final" and switched to it
  • Recreated migration with dnx ef migrations add InitialDatabase
  • Executed project and it worked

Upvotes: 1

Related Questions