'Enable-Migrations Exception calling "SetData" with "2" argument(s)
I created a library based on .NET 4.6.2 version.
To the library, I've added the EntityFramework version 6.1.3 package.
I created a model as follow
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Components.Models
{
public class Session
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
[Key]
[Required]
public string Identity { get; set; }
[Required]
public DateTime CreatedAt { get; set; }
[Required]
public DateTime UpdatedAt { get; set; }
}
}
And the dbcontext
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Components.Models;
namespace Components.DataContexts
{
public class SessionContext : DbContext
{
public SessionContext() : base(ConfigurationManager.ConnectionStrings["sessiondb"].ConnectionString)
{
}
public DbSet<Session> Sessions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
}
}
}
Then I tried to enable migration and did via
PM> Enable-Migrations
statement, got the error message:
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable."At D:\C#\IndustryCloud\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:720 char:5
+ $domain.SetData('startUpProject', $startUpProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project project, Int32 shellVersion)
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project project)
at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory)
at System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName)
at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.
What do I doing wrong?
Update Here the structure, how projet is build
In the sessiontest.cs, I wrote the test for db.
[Test]
public void InsertARow_DbInitial_ExpectDbValue()
{
var sn = new Session()
{
Identity = Random.Generate(15),
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
db.Sessions.Add(sn);
db.SaveChanges();
}
The ComponentsTest project, where I wrote the unit test, the app.config looks as follow:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Session;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" name="sessiondb" />
</connectionStrings>
</configuration>
And in the library(Component) itself the app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Session;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" name="sessiondb" />
</connectionStrings>
</configuration>
Solution 1:[1]
Other answers suggest this is an issue with your startup project.
As your project is a library, you could try setting your unit test project as your startup project per this answer.
You could also try setting the default project in Package Manager Console to your library project per the accepted answer here.
You might run into issues with where the migrations are created. If you need further control, in EF6 there are various arguments you can use with Enable-Migrations
as detailed in this answer but I don't have enough knowledge in this area to guide you further. You might need to do some reading.
Solution 2:[2]
2018 update - if the accepted answer doesn't help, see this github issue on the EF6 repository.
Apparently code migration commands don't work with the new project format. In order for the migrations commands to work, you need to create a Class Library (.NET Framework)
project (old standard), move all the files there, add all the needed dependencies and run the command.
EDIT: I just ran into this problem by creating a Class Library (.NET Standard)
project on VisualStudio 2017 15.6.6 using EntityFramework 6.2.0. Creating an "old standard" project as explained above fixes it.
Solution 3:[3]
To be free from defining startup project explicitly , you can use the command:
Enable-Migrations -EnableAutomaticMigrations -ProjectName Components -StartupProjectName Components
The parameters are:
-ProjectName
Specifies the project that the scaffolded migrations configuration class will be added to (configuration.cs). If omitted, the default project selected in package manager console is used.
-StartUpProjectName
Specifies the configuration file to use for named connection strings. If omitted, the specified project's configuration file is used.
To get more details for the command, run:
get-help enable-migrations -Full
Solution 4:[4]
This can happen if you have both Microsoft.EntityFrameworkCore.SqlServer installed alongside an older version of EntityFramework say 6.x
In Visual Studio, go to Project/Manage Nuget Packages...
Take a look through the Installed list and if more than one version of EntityFramework is installed, uninstall all older versions, leaving only the latest version.
Solution 5:[5]
This error occured to me today, after I cloned an old-ish project which was still configured to run on asp.net Core Rc2. After I installed 1.1 and changed all the variables the error persisted.
The solution in my case was to reboot. The installation of Asp.net Core 1.1 wasn't finished yet.
Solution 6:[6]
Edit: Simple fix (as stated above) -StartupProjectName YourEF6ProjectNameHere
When I had been using the wrong StartupProjectName I still got the error. That's when I was using the solution below:
My kludgy work-around is to unload the project that does not require EF migrations:
- Unload the startup project (in my case an ASP.Net Core project). Right-click the project name and selecting
Unload Project
. - Run the needed migration commands in the Package Manager Console
- Right-click the unloaded project and choose
Reload Project
- Right-click the same project and choose
Set as StartUp Project
.
Not fun.
Solution 7:[7]
if anyone is using visual studio 2017 then you might have to check for the following packages
Nuget Packages
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
after that run the migrations command below on console
EntityFrameworkCore\Enable-Migrations
EntityFrameworkCore\Add-Migration Book
Solution 8:[8]
I also had 2 versions of EF installed and using "EntityFrameworkCore\Add-Migration [migrationname]" command to specify which version to use solved the issue.
Solution 9:[9]
run:
"EntityFramework6\Enable-Migrations"
for Entity Framework 6. NB: use the version of your package installed.
Begin subsequent commands with: *EntityFramework6*
where EntityFramework is the package that has the libraries to perform our command.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | |
Solution 3 | M.Hassan |
Solution 4 | Simon |
Solution 5 | Marco |
Solution 6 | |
Solution 7 | Ă–mer |
Solution 8 | pcalkins |
Solution 9 | Djimatey Evans |