Asp.net 5.0 MVC6 EF6 Migration Scripts

asp.net-core asp.net-core-mvc entity-framework-6 powershell

Question

I am working on Asp.net 5.0 mvc6 and I am wanting to use entityframework 6 because 7 isn't completely coded yet and I have been able to get it to do everything but migration.

When I type enable-migration, add-migration or update-datatabase I get

enable-migration : The term 'enable-migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ enable-migration
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (enable-migration:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I'm pretty sure these commands are not in powershell. I did however find that there are tools in %userdir%.dnx\packages\EntityFramework\6.1.3\tools. Researching migrate.exe a little bit the little I did find they told me that it only works with proj files and so doesn't work with the new setup.

I have also tried to go the programming route with this code in the constructor of dbcontext I have:

        var configuration = new MigrationConfiguration();
        var migrator = new DbMigrator(configuration);
        migrator.Configuration.TargetDatabase = new DbConnectionInfo(nameOrConnectionString, "System.Data.SqlClient");
        if (migrator.GetPendingMigrations())
        {
            migrator.Update();
        }

and this code in my confirmation script:

    public MigrationConfiguration()
    {
        AutomaticMigrationsEnabled = true;
        MigrationsAssembly = Assembly.GetAssembly(typeof(InitialCreate));
        MigrationsNamespace = "[Namespace of my migration scripts]";
    }

Before I made any of this the database made a __migrationHistory table with an 201509291902537_InitialCreate in it, so I made one file with that name and made another called 201509291902538_test they both look like this:

201509291902537_InitialCreate:

namespace Infrastructure.EF.Migrations
{
    using System.Data.Entity.Migrations;

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
        }
    }
}

201509291902538_test:

namespace Infrastructure.EF.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class test: DbMigration
    {
        public override void Up()
        {
            Sql("insert into LegalEntity (Id, Name ) values(" + Guid.NewGuid() + ", 'Test'");
        }
    }
}

No matter what I tried migrator.GetPendingMigrations() never says it has any new updates and if I made a fake update and tell it exactly what it needs to update it still doesn't work it just throws a null reference exception on the update function.

Here's my fake migrator:

namespace Infrastructure.EF.Contexts
{
    public class Migrator : DbMigrator
    {
        public Migrator(DbMigrationsConfiguration configuration) : base(configuration)
        {
        }

        public override IEnumerable<string> GetDatabaseMigrations()
        {
            return new List<string>() { "InitialCreate" };
        }

        public override IEnumerable<string> GetLocalMigrations()
        {
            return new List<string>() { "InitialCreate", "test" };
        }

        public override IEnumerable<string> GetPendingMigrations()
        {
            return new List<string>() { "test" };
        }
    }
}

project.json:

{
    "version": "1.0.0-*",

    "dependencies": {
        "Autofac.Framework.DependencyInjection": "4.0.0-beta5-*",
        "AutoMapper": "4.0.4",
        "EntityFramework": "6.1.3",
        "log4net": "2.0.3",
        "Microsoft.AspNet.Http.Abstractions": "1.0.0-beta5",
        "Microsoft.CSharp": "4.0.0-*",
        "Microsoft.Framework.Configuration.Json": "1.0.0-beta5",
        "RestSharp": "105.2.3",
        "System.Linq": "4.0.0-*",
        "System.Runtime": "4.0.10-*",
        "System.Threading": "4.0.10-*"
    },

    "frameworks": {
        "dnx451": {
        }
    },

    "configurations": {
        "DV": { },
        "QA": { },
        "TR": { },
        "PR": { }
    }
}

I have tried it with both the class name and the file name and neither seems to work.

Has anybody had any luck with any of these things or see what I'm doing wrong with one of them?

1
8
10/5/2015 4:54:56 PM

Popular Answer

If you want to use EF6 with ASP.NET MVC6, then expose EF6 repositories as Web API 2 in .NET 4.5 and consume them in ASP.NET MVC6.

I know its lots of additional work, I followed this approach to learn and develop MVC6 applications because of existing EF6 data access layer and similar issues in EF6 to EF7 migrations.

1
10/9/2015 7:03:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow