例外实体框架代码优先迁移 [英] Exceptions for Entity Framework Code First Migrations
问题描述
我在使用实体框架4.3的Code First迁移越来越几个未处理的异常。
I'm getting several unhandled exceptions while using Code First Migrations of Entity Framework 4.3.
数据库方面:
public class MyAppContext : DbContext
{
public DbSet<Branch> Branches { get; set; }
public MyAppContext()
{ }
}
实体:
public class Branch : IEntity<Guid>
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Active { get; set; }
}
数据库初始化:
The database initializer:
public class MyAppInitializer : CreateDatabaseIfNotExists<MyAppContext>
{
protected override void Seed(MyAppContext context)
{
context.Branches.Add(new Branch() { Id = branchId, Name = "Acme", Description = "Acme", Active = true });
context.SaveChanges();
}
}
我安装实体框架4.3到我的DAL项目和MVC使用项目:
I installed Entity Framework 4.3 to my DAL project and MVC project using:
安装封装的EntityFramework
Install-Package EntityFramework
我已经设置了MVC项目为启动项目并执行以下命令来与数据库环境和初始化的DAL项目:
I have set the MVC project as the startup project and executed the following command to the DAL project with the database context and initializer:
PM>启用的迁移-Verbose
PM> Enable-Migrations -Verbose
使用的NuGet项目的Ckms.KeyManagement.Managers。
错误,同时寻找上下文类型(指定-Verbose看到异常详细信息)。
System.Data.Entity.Migrations.Design.ToolingException:无法加载请求的类型的一个或多个。获取更多信息,LoaderExceptions财产。在在
System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes
System.Data.Entity.Migrations.Design.ToolingFacade.Run(跑垒员
亚军)()结果$在
System.Data.Entity.Migrations.MigrationsCommands.FindContextToEnable()
编辑生成的配置b类$ b指定上下文
启用迁移。
码首先迁移项目Ckms.KeyManagement.Managers启用。
Using NuGet project 'Ckms.KeyManagement.Managers'.
Error while searching for context type (specify -Verbose to see exception details).
System.Data.Entity.Migrations.Design.ToolingException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. at
System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner
runner) at
System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes()
at
System.Data.Entity.Migrations.MigrationsCommands.FindContextToEnable()
Edit the generated Configuration class to specify the context to
enable migrations for.
Code First Migrations enabled for project Ckms.KeyManagement.Managers.
一个DbMigrationsConfiguration子类添加到项目DAL。如果我手动添加的DbContext的类型,并启用自动迁移:
A DbMigrationsConfiguration child class is added to the DAL project. If I add the type of the DbContext manually and enable Automatic Migrations:
internal sealed class Configuration : DbMigrationsConfiguration<MyAppContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(MyAppContext context)
{ }
}
这些异常抛出外接迁移和更新,数据库命令:
These exceptions are thrown for the Add-Migration and Update-Database commands:
PM>添加迁移TestEFMigrationsColumn -Verbose
PM> Add-Migration TestEFMigrationsColumn -Verbose
使用的NuGet项目
'Ckms.KeyManagement.Managers。使用启动项目'。
System.Reflection.TargetInvocationException:异常已被调用的目标引发异常
。 ---> System.ArgumentException:在
参数不正确。 (异常来自HRESULT:80070057
(E_INVALIDARG))---内部异常堆栈跟踪的结尾---在
System.RuntimeType.InvokeDispMethod(字符串名称,的BindingFlags
invokeAttr,对象目标, [对象]在
System.RuntimeType.InvokeMember ARGS,布尔[] byrefModifiers,
的Int32文化的String [] namedParameters)(字符串名称,的BindingFlags
的BindingFlags,活页夹粘结剂,目标对象,对象[] providedArgs,
ParameterModifier []修饰符,CultureInfo的文化的String []
namedParams)在
System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,
对象[] 。参数)异常已由
调用的目标引发异常
Using NuGet project 'Ckms.KeyManagement.Managers'. 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.
更新 - 数据库:
PM>更新,数据库-Verbose
PM> Update-Database -Verbose
使用的NuGet项目
'Ckms.KeyManagement。经理人。使用启动项目'。
System.Reflection.TargetInvocationException:异常已被调用的目标引发异常
。 ---> System.ArgumentException:在
参数不正确。 (异常来自HRESULT:80070057
(E_INVALIDARG))---内部异常堆栈跟踪的结尾---在
System.RuntimeType.InvokeDispMethod(字符串名称,的BindingFlags
invokeAttr,对象目标, [对象]在
System.RuntimeType.InvokeMember ARGS,布尔[] byrefModifiers,
的Int32文化的String [] namedParameters)(字符串名称,的BindingFlags
的BindingFlags,活页夹粘结剂,目标对象,对象[] providedArgs,
ParameterModifier []修饰符,CultureInfo的文化的String []
namedParams)在
System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,
对象[]参数)异常已由
调用的目标引发异常。
Using NuGet project 'Ckms.KeyManagement.Managers'. 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.
任何想法?错误消息是不是真的有帮助。我曾尝试的NuGet使用和不使用现有的数据库命令。
Any ideas? The error messages are not really helpful. I have tried the Nuget commands with and without an existing database.
推荐答案
如果您正在使用的数据访问单独的库需要提供运行的查询时,它的名字:
If you are using separate library for data access you need to provide it name when running query:
的添加迁移-StartUpProjectName你DAL工程MyNewMigration 的
的更新 - 数据库-StartUpProjectName你DAL工程-Verbose 的
这篇关于例外实体框架代码优先迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!