Exceptions for Entity Framework Code First Migrati

2019-04-07 20:34发布

问题:

I'm getting several unhandled exceptions while using Code First Migrations of Entity Framework 4.3.

The database context:

public class MyAppContext : DbContext
{
   public DbSet<Branch> Branches { get; set; }

   public MyAppContext()
   { }
}

The entity:

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();
   }
}

I installed Entity Framework 4.3 to my DAL project and MVC project using:

Install-Package EntityFramework

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> Enable-Migrations -Verbose

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.

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> Add-Migration TestEFMigrationsColumn -Verbose

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.

Update-Database:

PM> Update-Database -Verbose

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.

Any ideas? The error messages are not really helpful. I have tried the Nuget commands with and without an existing database.

回答1:

If you are using separate library for data access you need to provide it name when running query:

Add-Migration -StartUpProjectName "Your DAL Project" MyNewMigration

Update-Database -StartUpProjectName "Your DAL Project" -Verbose



回答2:

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

First: Name of Migration

Dbset.Framework: Project where dbContext and other classes

CodeFirstConsole: Start Up project (could be your web, windows or console app)



回答3:

For System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) adding -projectname and startupprojectname did not help.

Setting the PackageManager Console's "Default Project" Dropdown to point to the Library (in my case) where I wanted the "Migration folder" and its expected contents to be was the only way to get this running from a multiproject solution.



回答4:

I also had the same issue. Found out that if anything is wrong with the config files this error comes up. I had duplicate tags in web.config and removing these solved my issue.



回答5:

I had solve this problem only by changing the name used in connection string.

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

And I use connectionStrings after closing tag of the

appSettings

and just before starting tag of

system.web

Make sure that name that you use in connectionString not used in other connections.



回答6:

Ran into the same problem , solved by removing <globalization> from web.config.