在sperate项目中添加DbContext时迁移失败

时间:2015-08-07 21:58:14

标签: c# entity-framework migration asp.net-core-mvc

我有一个包含不同图层的项目:网络,服务,模型,数据,每个人在同一个解决方案中都有不同的项目。应用程序,编译并运行正常。但是当我尝试实现迁移时,我收到了以下错误

dnx。如果迁移添加了MigrationFile

System.InvalidOperationException: No DbContext was found. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
   at Microsoft.Data.Entity.Commands.ContextTool.SelectType(IEnumerable`1 types, String name)
   at Microsoft.Data.Entity.Commands.MigrationTool.GetContextType(String name)
   at Microsoft.Data.Entity.Commands.MigrationTool.AddMigration(String migrationName, String contextTypeName, String startupAssemblyName, String rootNamespace,String projectDir)
   at Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass12_0.<AddMigration>b__0()
   at Microsoft.Data.Entity.Commands.Program.Execute(String startupProject, Func`1 invoke)
   at Microsoft.Framework.Runtime.Common.CommandLine.CommandLineApplication.Execute(String[] args)
--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
   at Microsoft.Framework.ApplicationHost.Program.ExecuteMain(DefaultHost host,String applicationName, String[] args)
   at Microsoft.Framework.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---

   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 args, IRuntimeEnvironment env, FrameworkName targetFramework)
   at dnx.host.RuntimeBootstrapper.ExecuteAsync(String[] args, FrameworkName targetFramework)
   at dnx.host.RuntimeBootstrapper.Execute(String[] args, FrameworkName targetFramework)

2 个答案:

答案 0 :(得分:0)

I'm using this answer as a reference

也许您的项目有多个DbContext,或者您没有启用迁移。

如果您有多个Context,则需要分别为每个Context启用和添加迁移:

add-migration -ConfigurationTypeName MyProject.MigrationsFolder.Configuration "migrationName"

此代码将根据您的Context添加新的Migration并使用与其关联的Configuration类。以下代码将更新与Configuration类关联的数据库。

update-database -ConfigurationTypeName MyProject.MigrationsFolder.Configuration

答案 1 :(得分:0)

命令必须是这样的   - dnu恢复

  • cd包含上下文路径的项目

  • dnx。 ef migration add -c ContextName - s StartupProjectName

尝试一下,如果这项工作让我知道,thnx ^^