I get a few unhandled exceptions when using Code First Migrations of Entity Framework 4.3.
Database Context:
public class MyAppContext : DbContext { public DbSet<Branch> Branches { get; set; } public MyAppContext() { } }
Essence:
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; } }
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 in a DAL project and an MVC project using:
Install-Package EntityFramework
I installed the MVC project as a startup project and ran the following command for the DAL project with the database context and initializer:
PM> Enable-Migrations -Verbose
Using the NuGet project 'Ckms.KeyManagement.Managers'. Error finding context type (specify -Verbose to view exception details). System.Data.Entity.Migrations.Design.ToolingException: Cannot load one or more of the requested types. Get the LoaderExceptions property for more information. in System.Data.Entity.Migrations.Design.ToolingFacade.Run (BaseRunner runner) in System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes ()
in System.Data.Entity.Migrations.MigrationsCommands.FindContextToEnable () Edit the generated configuration class to indicate the context to enable migration for. First launch of Migrations for the Ckms.KeyManagement.Managers project.
A child class DbMigrationsConfiguration has been added to the DAL project. If I add the DbContext type manually and enable auto-move:
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 the NuGet project 'Ckms.KeyManagement.Managers'. Using the StartUp project. '' System.Reflection.TargetInvocationException: An exception was thrown on the target of the call. ---> System.ArgumentException: parameter is invalid. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) --- End of internal trace of the exception stack --- at System.RuntimeType.InvokeDispMethod (string name, BindingFlags invokeAttr, Object target, Object [] args, Boolean [] byrefModifiers, Culture Int32, String [] namedParameters) in System.RuntimeType.InvokeMember (string name, BindingFlags bindingFlags, Binder binder, Object target, Object [] providedArgs, ParameterModifier [] modifiers, CultureInfo culture, String [] namedParams) in System.Management.Automation.ComMethod .InvokeMethod (PSMethod method, Object []). An exception was chosen as the target of the challenge.
Update-Database:
PM> Update-Database-Verbosa
Using the NuGet project 'Ckms.KeyManagement.Managers'. Using the StartUp project. '' System.Reflection.TargetInvocationException: An exception was thrown on the target of the call. ---> System.ArgumentException: parameter is invalid. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) --- End of internal trace of the exception stack --- at System.RuntimeType.InvokeDispMethod (string name, BindingFlags invokeAttr, Object target, Object [] args, Boolean [] byrefModifiers, Culture Int32, String [] namedParameters) in System.RuntimeType.InvokeMember (string name, BindingFlags bindingFlags, Binder binder, Object target, Object [] providedArgs, ParameterModifier [] modifiers, CultureInfo culture, String [] namedParams) in System.Management.Automation.ComMethod .InvokeMethod (PSMethod method, Object []). An exception was chosen as the target of the challenge.
Any ideas? Error messages are not very helpful. I tried Nuget commands with and without an existing database.