我正在研究 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/