entity framework代码优先迁移的例外情况
我在使用Entity Framework 4.3的Code First Migrations时遇到了几个未处理的exception。
数据库上下文:
public class MyAppContext : DbContext { public DbSet Branches { get; set; } public MyAppContext() { } }
实体:
public class Branch : IEntity { public Guid Id { get; set; } public string Name { get; set; } public string Description { get; set; } public bool Active { get; set; } }
数据库初始化程序:
public class MyAppInitializer : CreateDatabaseIfNotExists { 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项目中:
Install-Package EntityFramework
我已将MVC项目设置为启动项目,并使用数据库上下文和初始化程序对DAL项目执行以下命令:
PM>启用 – 迁移-Verbose
使用NuGet项目’Ckms.KeyManagement.Managers’。 搜索上下文类型时出错(指定-Verbose以查看exception详细信息)。 System.Data.Entity.Migrations.Design.ToolingException:无法加载一个或多个请求的类型。 检索LoaderExceptions属性以获取更多信息。 System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes()中的System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.MigrationsCommands.FindContextToEnable()编辑生成的Configuration类以指定启用迁移的上下文。 为项目Ckms.KeyManagement.Managers启用了代码优先迁移。
DbMigrationsConfiguration子类被添加到DAL项目中。 如果我手动添加DbContext的类型并启用自动迁移:
internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(MyAppContext context) { } }
对Add-Migration和Update-Database命令抛出这些exception:
PM>添加迁移TestEFMigrationsColumn -Verbose
使用NuGet项目’Ckms.KeyManagement.Managers’。 使用StartUp项目”。 System.Reflection.TargetInvocationException:调用目标抛出了exception。 —> System.ArgumentException:参数不正确。 (来自HRESULT的exception:0x80070057(E_INVALIDARG))—内部exception堆栈跟踪结束—在System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,Int32 culture ,String [] namedParameters)在System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)在System.Management.Automation。 ComMethod.InvokeMethod(PSMethod方法,Object []参数)调用的目标抛出了exception。
更新数据库:
PM>更新 – 数据库-Verbose
使用NuGet项目’Ckms.KeyManagement.Managers’。 使用StartUp项目”。 System.Reflection.TargetInvocationException:调用目标抛出了exception。 —> System.ArgumentException:参数不正确。 (来自HRESULT的exception:0x80070057(E_INVALIDARG))—内部exception堆栈跟踪结束—在System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,Int32 culture ,String [] namedParameters)在System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)在System.Management.Automation。 ComMethod.InvokeMethod(PSMethod方法,Object []参数)调用的目标抛出了exception。
有任何想法吗? 错误消息并不真正有用。 我已经尝试过使用和不使用现有数据库的Nuget命令。
如果您使用单独的库进行数据访问,则需要在运行查询时提供它的名称:
添加迁移-StartUpProjectName“您的DAL项目”MyNewMigration
Update-Database -StartUpProjectName“你的DAL项目”-Verbose
add-migration -Name First -ProjectName DbSet.Framework -StartUpProjectName CodeFirstConsole
第一:迁移名称
Dbset.Framework:项目所在的dbContext和其他类
CodeFirstConsole:启动项目(可能是您的Web,Windows或控制台应用程序)
对于System.ArgumentException:参数不正确。 (来自HRESULT的exception:0x80070057(E_INVALIDARG))添加-projectname和startupprojectname没有帮助。
设置PackageManager控制台的“默认项目”下拉列表指向库(在我的情况下)我想要“迁移文件夹”及其预期内容是从多项目解决方案运行的唯一方法。
我也有同样的问题。 发现如果配置文件有任何问题,则会出现此错误。 我在web.config中有重复的标签,删除这些标签解决了我的问题。
我只是通过更改连接字符串中使用的名称来解决此问题。
我在关闭标签后使用connectionStrings
appSettings
就在开始标记之前
system.web
确保您在connectionString
使用的名称未在其他连接中使用。
遇到同样的问题,通过从web.config中删除
来解决。