为什么启用迁移失败?
今天早上我安装了Visual Studio 2015社区版。之后,我创建了一个新的ASP.NET Web应用程序,并在“ASP.NET 5预览模板”部分中选择了“Web应用程序”模板。为什么启用迁移失败?
创建项目后,我加入了第二个项目,其中应该包含我所有的实体(EF代码第一次),业务对象,公用事业及其他核心员工的溶液(类库)。
的类库使用的.NET Framework 4.5.2和引用的EntityFramework 6.1.3。我还添加了ASP.NET Identity 2.2.1(包括EntityFramework提供程序)。
在App.config中我有这样的连接字符串:
<connectionStrings>
<add name="ApplicationDbContext" connectionString="Server=(localdb)\projectsv12;Database=myDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
这里是相应的数据库上下文类:
public class ApplicationDbContext : IdentityDbContext<User> {
public ApplicationDbContext() : base("name=ApplicationDbContext") { }
public static ApplicationDbContext Create() {
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
}
public System.Data.Entity.DbSet<Branch> Branches { get; set; }
public System.Data.Entity.DbSet<Dimension> Dimensions { get; set; }
}
正如你所看到的连接字符串的名称是相同的App.config和DbContext构造函数。我还设置了两个POCO实体并将它们添加到DbContext(分支和维度)中。
一切都应该准备“启用的迁移”,然后“更新数据库”正如我多次与Visual Studio做到了2013年。
然而,在Visual Studio 2015年,我收到一条错误信息。这是我做的:
- 我拉起包管理器控制台。
- 我在“Default project”下拉列表中选择了我的Class Library项目。
- 我进入了“启用的迁移”在命令提示符下(我也尝试了新的“添加迁移”附带EF7 - 因为EF7在我的Web项目,我虽然我会试试看引用)。
以下是错误消息我得到:
异常调用 “的SetData” 与 “2” 参数(S):“类型“Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package。 Automation.OAProject”在组件‘Microsoft.VisualStudio.ProjectSystem.VS.Implementation,版本= 14.0.0.0,文化=中性公钥= b03f5f7f11d50a3a’未标记为可序列“。在C:\ Tfs \ Dev \ primaVISTA \ packages \ EntityFramework.6.1.3 \ tools \ EntityFramework.psm1:720 char:5 + $ domain.SetData('startUpProject',$ startUpProject) + ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified :(:) [],MethodInvocationException + FullyQualifiedErrorId:SerializationException System.NullReferenceException:对象不设置为一个对象的一个实例。 at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project project,Int32 shellVersion) at System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project project) at System.Data.Entity.Migrations .MigrationsDomainCommand.GetFacade(String configurationTypeName,Boolean useContextWorkingDirectory) at System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName) at System.Data.Entity.Migrations.EnableMigrationsCommand。 <> c__DisplayClass2。 <。> b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand。执行(操作命令) 未将对象引用设置为对象的实例。
有没有人知道我做错了什么?我错过了什么?或者,这是由于VS2015中的错误?
我真的很感谢任何帮助!谢谢!!
正如Sirwan建议的那样,我打开了一个命令提示符,并将自己的方式放到了我的库项目文件夹中。在这里,我执行添加迁移命令:
C:\Tfs\Dev\primaVISTA\src\primaVISTA.Core>dnx . ef migration add M1
System.InvalidOperationException:无法解析项目 'primaVISTA.Core' 从C:\ TFS \开发\ primaVISTA的\ src \ primaVISTA.Core 在Microsoft.Framework.Runtime .ApplicationHostContext..ctor(的IServiceProvider的ServiceProvider,字符串projectDirectory,字符串packagesDirectory,字符串配置,FrameworkName targetFramework,ICACHE缓存,ICacheContextAccessor cacheContextAccessor,INamedCacheDependencyProvider namedCacheDependencyProvider,IAssemblyLoadContextFactory loadContextFactory,布尔skipLockFileValidation) 在Microsoft.Framework.Runtime.DefaultHost.Initialize(DefaultHostOptions选项,IServiceProvider hostServices) at Microsoft.F ramework.Runtime.DefaultHost..ctor(DefaultHostOptions选项,IServiceProvider hostServices) at Microsoft.Framework.ApplicationHost.Program.Main(String [] args) ---上一个位置抛出异常的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly,String [] args,IServiceProvider serviceProvider) at dnx.host.Bootstrapper.RunAsync(List` 1个指定参数时,IRuntimeEnvironment ENV,FrameworkName targetFramework) 在dnx.host.RuntimeBootstrapper.ExecuteAsync(字串[] args,FrameworkName targetFramework) 在dnx.host.RuntimeBootstrapper.Execute(字串[] args,FrameworkName targetFramework)
我不知道这是什么意思?
这不是目前在Visual Studio 2015年的情况下,你应该键入您的项目目录下(不是您的解决方案目录):
dnx . ef [options] [command]
要查看子命令可用于迁移命令,dnx . ef migration --help
类型:
add
- 添加新的迁移apply
- 应用迁移到数据库-
list
- 列出迁移 -
script
- 生成迁移 -
remove
SQL脚本 - 删除最后一个迁移
更新:所有的 首先,你必须设置你的环境:
- 打开一个新的命令提示符。
- 导航到src [ProjectName]目录。
- 运行dnvm升级以安装必要的ASP.NET 5工具,如果尚未完成。
- 运行dnu restore加载项目所需的所有软件包。
然后运行这个命令:
dnx . ef migration add firstMigration
dnx . ef migration apply
我试图按照您的建议 - 并相应更新我原来的职位(请参见上文) – Ingmar
好吧,我想,我有一个一般的误解:如果我执行“dnx。ef migration add M1”不在我的图书馆项目中,但在我的web项目中,一切都很好。然后,在运行“dnx.ef migration apply”之后,我的数据库被创建。欢呼!但是:它只包含ASP.NET Identity的实体。我的自定义实体(在我的库项目中定义)都没有在数据库中创建。 – Ingmar
@IngmarBode我已经更新了我的答案,查看我的答案 –