每次我需要添加迁移或更新数据库时使用EF 4.3.1迁移时,我必须使用StartUpProject参数。我可以将它设置在某个地方,所以我不必继续这样做吗?我不想将我的数据项目设置为启动项目。如果有所不同,我的上下文和迁移将在不同的项目中进行。
不使用StartpProject参数时抛出异常:
update-database -verbose
Using NuGet project 'Data.Deployment'.
Using StartUp project ''.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
Exception has been thrown by the target of an invocation.
答案 0 :(得分:8)
至少在Entity Framework 6中,要指定启动项目,请在update-database中使用-StartUpProjectName
参数:
update-database -StartUpProjectName <String>
您可以使用程序包管理器控制台中的get-help
命令查看命令的可能参数:
get-help update-database -detailed
答案 1 :(得分:2)
我不确定这是否就是您所说的,但您可以将程序包管理器控制台中的默认项目设置为迁移配置所在的项目;这是最右边的下拉列表 这样我就不必将启动项目作为参数提供。
答案 2 :(得分:1)
解决这个问题的方法是将测试项目附加到解决方案中。
即使您不想编写大量单元测试,它也是数据层命名空间或类似命名的适当启动项目。
这使您的启动问题更加清晰,包管理器适用于整个解决方案IIRC - 因此请确保默认项目是包含迁移文件夹的项目。
答案 3 :(得分:0)
使用StartupProject参数:Update-database -StartupProject "PROJECTNAME"