【发布时间】:2012-03-23 20:07:46
【问题描述】:
我在使用 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; }
}
数据库初始化器:
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();
}
}
我使用以下方法将 Entity Framework 4.3 安装到我的 DAL 项目和 MVC 项目中:
安装包EntityFramework
我已将 MVC 项目设置为启动项目,并使用数据库上下文和初始化程序对 DAL 项目执行以下命令:
PM> Enable-Migrations -Verbose
使用 NuGet 项目“Ckms.KeyManagement.Managers”。 搜索上下文类型时出错(指定 -Verbose 以查看异常详细信息)。 System.Data.Entity.Migrations.Design.ToolingException:无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。在 System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner 亚军)在 System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes()
在 System.Data.Entity.Migrations.MigrationsCommands.FindContextToEnable() 编辑生成的配置类以指定上下文 启用迁移。 为项目 Ckms.KeyManagement.Managers 启用 Code First 迁移。
DbMigrationsConfiguration 子类被添加到 DAL 项目中。如果我手动添加 DbContext 的类型并启用自动迁移:
internal sealed class Configuration : DbMigrationsConfiguration<MyAppContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(MyAppContext context)
{ }
}
Add-Migration 和 Update-Database 命令会引发这些异常:
PM> 添加迁移测试EFMigrationsColumn -Verbose
使用 NuGet 项目 'Ckms.KeyManagement.Managers'。使用启动项目''。 System.Reflection.TargetInvocationException:已引发异常 通过调用的目标。 ---> System.ArgumentException: 参数不正确。 (来自 HRESULT 的异常:0x80070057 (E_INVALIDARG)) --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeType.InvokeDispMethod(字符串名称,BindingFlags invokeAttr,对象目标,Object[] args,Boolean[] byrefModifiers, Int32 文化,String[] namedParameters) 在 System.RuntimeType.InvokeMember(字符串名称,BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] 修饰符、CultureInfo 文化、String[] 命名参数)在 System.Management.Automation.ComMethod.InvokeMethod(PSMethod 方法, Object[] arguments) 的目标已抛出异常 调用。
更新数据库:
PM> 更新-数据库-详细
使用 NuGet 项目 'Ckms.KeyManagement.Managers'。使用启动项目''。 System.Reflection.TargetInvocationException:已引发异常 通过调用的目标。 ---> System.ArgumentException: 参数不正确。 (来自 HRESULT 的异常:0x80070057 (E_INVALIDARG)) --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeType.InvokeDispMethod(字符串名称,BindingFlags invokeAttr,对象目标,Object[] args,Boolean[] byrefModifiers, Int32 文化,String[] namedParameters) 在 System.RuntimeType.InvokeMember(字符串名称,BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] 修饰符、CultureInfo 文化、String[] 命名参数)在 System.Management.Automation.ComMethod.InvokeMethod(PSMethod 方法, Object[] arguments) 的目标已抛出异常 调用。
有什么想法吗?错误消息并没有真正的帮助。我已经尝试过使用和不使用现有数据库的 Nuget 命令。
【问题讨论】:
标签: c# entity-framework entity-framework-migrations