例外实体框架代码优先迁移

编程入门 行业动态 更新时间:2024-10-26 08:34:18
本文介绍了例外实体框架代码优先迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用实体框架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 的

更多推荐

例外实体框架代码优先迁移

本文发布于:2023-07-27 12:08:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!