powershell - Asp.net 5.0 MVC6 EF6 迁移脚本

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

我正在研究 Asp.net 5.0 mvc6,我想使用 entityframework 6,因为 7 还没有完全编码,我已经能够让它做除了迁移之外的所有事情。

当我输入 enable-migration、add-migration 或 update-database 时,我得到

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

我很确定这些命令不在 powershell 中。但是我确实发现 %userdir%.dnx\packages\EntityFramework\6.1.3\tools 中有工具。对 migrate.exe 进行了一点研究,我发现他们告诉我它只适用于 proj 文件,因此不适用于新设置。

我还尝试在 dbcontext 的构造函数中使用此代码进行编程:
        var configuration = new MigrationConfiguration();
        var migrator = new DbMigrator(configuration);
        migrator.Configuration.TargetDatabase = new DbConnectionInfo(nameOrConnectionString, "System.Data.SqlClient");
        if (migrator.GetPendingMigrations())
        {
            migrator.Update();
        }

以及我的确认脚本中的这段代码:
    public MigrationConfiguration()
    {
        AutomaticMigrationsEnabled = true;
        MigrationsAssembly = Assembly.GetAssembly(typeof(InitialCreate));
        MigrationsNamespace = "[Namespace of my migration scripts]";
    }

在我做任何这些之前,数据库做了一个 __migrationHistory 表,里面有一个 201509291902537_InitialCreate,所以我用这个名字制作了一个文件,并制作了另一个名为 201509291902538_test 的文件,它们都如下所示:

201509291902537_InitialCreate:
namespace Infrastructure.EF.Migrations
{
    using System.Data.Entity.Migrations;

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

201509291902538_测试:
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'");
        }
    }
}

无论我尝试什么 migrator.GetPendingMigrations() 都不会说它有任何新的更新,如果我做了一个假更新并告诉它它需要更新什么它仍然不起作用它只是在更新函数上抛出一个空引用异常.

这是我的假移民:
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" };
        }
    }
}

项目.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": { }
    }
}

我用类名和文件名都试过了,但似乎都不起作用。

有没有人对这些事情有任何运气,或者看到我在其中之一做错了什么?

最佳答案

如果您想将 EF6 与 ASP.NET MVC6 一起使用,则在 .NET 4.5 中将 EF6 存储库公开为 Web API 2,并在 ASP.NET MVC6 中使用它们。

我知道它有很多额外的工作,我遵循这种方法来学习和开发 MVC6 应用程序,因为现有的 EF6 数据访问层和 EF6 到 EF7 迁移中的类似问题。

关于powershell - Asp.net 5.0 MVC6 EF6 迁移脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589608/

相关文章:

PowerShell 捕获写入主机输出

variables - 为什么会这样(或如何)?

xml - 在PowerShell中对获取内容数据使用-f字符串格式

c# - 带有 Oracle 的 EntityFramework 6 : Is there a "standalone" ODAC (i. e。没有 ODBC),并且每台客户端 PC 都需要安装它吗?

azure - ASP.NET Core 的身份验证中间件是否始终对 OpenID Connect 使用隐式流?

powershell - 单行中的Send-MailMessage命令不起作用

c# - 具有不变名称 'MySql.Data.MySqlClient' 的 ADO.NET 提供程序未在计算机或应用程序配置文件中注册

entity-framework - Entity Framework : Sharing entities across different DbContexts

c# - 不支持连接字符串关键字 'server'

c# - 无法使用 EFCore、EntityState.Modified : "Database operation expected to affect 1 row(s) but actually affected 0 row(s)." 编辑数据库条目