Enable-Migrations异常调用“ SetData”;与“ 2”论点 [英] Enable-Migrations Exception calling "SetData" with "2" argument(s)

查看:65
本文介绍了Enable-Migrations异常调用“ SetData”;与“ 2”论点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个基于.NET 4.6.2版本的库。

向该库中,添加了EntityFramework版本6.1.3程序包。

我创建了一个模型遵循

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

和dbcontext

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

语句,收到错误消息:

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.

我在做什么错?

更新
这里的结构,如何构建projet

Update Here the structure, how projet is build

在sessiontest.cs中,我为db编写了测试。

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

}

ComponentTest项目是我编写单元测试的地方, app.config如下所示:

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>

然后在library(Component)本身中执行app.config:

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>


推荐答案

其他答案表明这与您的启动项目有关

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.

您还可以尝试在Package Manager控制台中设置默认项目请按此处进入您的图书馆项目。

You could also try setting the default project in Package Manager Console to your library project per the accepted answer here.

您可能会在创建迁移位置时遇到问题。如果需要进一步控制,则可以在EF6中与 Enable-Migrations 一起使用各种参数,如此答案,但我在此领域没有足够的知识来进一步指导您。您可能需要阅读一些东西。

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.

这篇关于Enable-Migrations异常调用“ SetData”;与“ 2”论点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆