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

时间:2012-03-06 10:48:41

标签: c# entity-framework ef-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&GT;启用 - 迁移-Verbose

     

使用NuGet项目'Ckms.KeyManagement.Managers'。   搜索上下文类型时出错(指定-Verbose以查看异常详细信息)。   System.Data.Entity.Migrations.Design.ToolingException:无法加载一个或多个请求的类型。检索LoaderExceptions属性以获取更多信息。在   System.Data.Entity.Migrations.Design.ToolingFacade.Run(跑垒员   跑者)   System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes()
  在   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&GT;添加迁移TestEFMigrationsColumn -Verbose

     

使用NuGet项目   'Ckms.KeyManagement.Managers'。使用StartUp项目''。   System.Reflection.TargetInvocationException:抛出了异常   通过调用的目标。 ---&GT; System.ArgumentException:The   参数不正确。 (HRESULT的例外情况:0x80070057   (E_INVALIDARG))---内部异常堆栈跟踪结束--- 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 []修饰符,CultureInfo文化,String []   namedParams)at   System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,   Object [] arguments)目标抛出了异常   调用

更新-数据库:

  

PM&GT; Update-Database -Verbose

     

使用NuGet项目   'Ckms.KeyManagement.Managers'。使用StartUp项目''。   System.Reflection.TargetInvocationException:抛出了异常   通过调用的目标。 ---&GT; System.ArgumentException:The   参数不正确。 (HRESULT的例外情况:0x80070057   (E_INVALIDARG))---内部异常堆栈跟踪结束--- 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 []修饰符,CultureInfo文化,String []   namedParams)at   System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,   Object [] arguments)目标抛出了异常   调用

有什么想法吗?错误消息并不真正有用。我已经尝试过使用和不使用现有数据库的Nuget命令。

7 个答案:

答案 0 :(得分:12)

如果您使用单独的库进行数据访问,则需要在运行查询时提供其名称:

Add-Migration -StartUpProjectName“您的DAL项目”MyNewMigration

Update-Database -StartUpProjectName“您的DAL项目”-Verbose

答案 1 :(得分:4)

add-migration -Name First -ProjectName DbSet.Framework -StartUpProjectName CodeFirstConsole

第一名:迁移名称

Dbset.Framework:项目所在的dbContext和其他类

CodeFirstConsole:启动项目(可能是您的网站,Windows或控制台应用)

答案 2 :(得分:3)

对于System.ArgumentException:参数不正确。 (来自HRESULT的异常:0x80070057(E_INVALIDARG))添加-projectname和startupprojectname没有帮助。

设置PackageManager控制台&#34;默认项目&#34;下拉指向图书馆(在我的情况下),我想要&#34;迁移文件夹&#34;并且它的预期内容是从多项目解决方案中运行的唯一方法。

答案 3 :(得分:1)

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

答案 4 :(得分:1)

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

<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中使用的名称未在其他连接中使用。

答案 5 :(得分:0)

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

答案 6 :(得分:0)

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