Exceptions to Entity First Migrations Base Code

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.

+6
source share
5 answers

If you use a separate library to access data, you need to specify its name when starting the request:

Add-Migration -StartUpProjectName "Your DAL Project" MyNewMigration

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

+12
source
 add-migration -Name First -ProjectName DbSet.Framework -StartUpProjectName CodeFirstConsole 

First: migration name

Dbset.Framework: a project where dbContext and other classes

CodeFirstConsole project: launch (may be your web window or console application)

+4
source

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

Setting the PackageManager "Project Default" console for the drop-down list to point to the library (in my case) where I wanted the "transfer folder" and its expected contents to be the only way to get this from a multi-project solution.

+3
source

I had the same problem too. It turned out that if something is wrong with the configuration files, this error occurs. I had duplicate tags in web.config and resolved them.

+1
source

The same problem is solved by removing <globalization> from web.config.

0
source

Source: https://habr.com/ru/post/910066/


All Articles