C# – Exceptions for Entity Framework Code First Migrations

centity-frameworkentity-framework-migrations

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.

Best Answer

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

Related Topic