Enable-Migrations Exception calling "SetData" with "2" argument(s)

c# entity-framework

Question

Using the.NET 4.6.2 version as a foundation, I constructed a library.
I've included the EntityFramework version 6.1.3 package in the library.
I developed the following model.

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

The dbcontext is also

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

  }
}

I then attempted to activate migration through

PM> Enable-Migrations

statement, we encountered an error:

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.

Why am I misusing this?

Update The project's structure is shown here.

enter image description here

I created the test for database in sessiontest.cs.

[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 app.config in the ComponentsTest project, where I created the unit test, looks as follows:

<?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 the app.config is located in the library (component) itself:

<?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>
1
44
7/18/2016 12:56:05 PM

Accepted Answer

Other responses imply that there is a problem with your new project.

You may try making your unit test project your starter project in accordance with this response as your project is a library.

As suggested in the acceptable answer here, you may also try making your library project the default project in the Package Manager Console.

The location where the migrations are made may cause problems for you. If you need further control, in EF6 there are various arguments you can use withEnable-Migrations as explained in this response, but I don't know enough about this topic to be able to provide you further advice. You may have to read something.

41
5/23/2017 12:10:10 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow