繁体   English   中英

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

[英]Exceptions for Entity Framework Code First Migrations

在使用Entity Framework 4.3的Code First Migrations时,我得到了几个未处理的异常。

数据库上下文:

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项目中:

Install-Package EntityFramework

我已将MVC项目设置为启动项目,并使用数据库上下文和初始化程序对DAL项目执行以下命令:

PM>启用 - 迁移--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(BaseRunner runner)
在System.Data.Entity.Migrations.MigrationsCommands.FindContextToEnable()编辑生成的Configuration类以指定启用迁移的上下文。 为项目Ckms.KeyManagement.Managers启用了代码优先迁移。

DbMigrationsConfiguration子类被添加到DAL项目中。 如果我手动添加DbContext的类型并启用自动迁移:

internal sealed class Configuration : DbMigrationsConfiguration<MyAppContext>
{
   public Configuration()
   {
      AutomaticMigrationsEnabled = true;
   }

   protected override void Seed(MyAppContext context)
   { }
}

对Add-Migration和Update-Database命令抛出这些异常:

PM>添加迁移TestEFMigrationsColumn -Verbose

使用NuGet项目'Ckms.KeyManagement.Managers'。 使用StartUp项目''。 System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---> System.ArgumentException:参数不正确。 (来自HRESULT的异常:0x80070057(E_INVALIDARG))---内部异常堆栈跟踪结束---在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 []参数)调用的目标抛出了异常。

更新数据库:

PM>更新 - 数据库-Verbose

使用NuGet项目'Ckms.KeyManagement.Managers'。 使用StartUp项目''。 System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---> System.ArgumentException:参数不正确。 (来自HRESULT的异常:0x80070057(E_INVALIDARG))---内部异常堆栈跟踪结束---在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 []参数)调用的目标抛出了异常。

有任何想法吗? 错误消息并不真正有用。 我已经尝试过使用和不使用现有数据库的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的异常:0x80070057(E_INVALIDARG))添加-projectname和startupprojectname没有帮助。

设置PackageManager控制台的“默认项目”下拉列表指向库(在我的情况下)我想要“迁移文件夹”及其预期内容是从多项目解决方案运行的唯一方法。

我也有同样的问题。 发现如果配置文件有任何问题,则会出现此错误。 我在web.config中有重复的标签,删除这些标签解决了我的问题。

我只是通过更改连接字符串中使用的名称来解决此问题。

<add name="abcd" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />

我在关闭标签后使用connectionStrings

appSettings

就在开始标记之前

system.web

确保您在connectionString使用的名称未在其他连接中使用。

遇到同样的问题,通过从web.config中删除<globalization>来解决。

您的网络中必须有两个连接字符串。 配置文件。 只需删除一个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM