gpt4 book ai didi

c# - 使用 "SetData"参数调用 "2"的启用迁移异常

转载 作者:IT王子 更新时间:2023-10-29 03:56:20 25 4
gpt4 key购买 nike

我创建了一个基于 .NET 4.6.2 版本的库。
在库中,我添加了 EntityFramework 版本 6.1.3 包。
我创建了一个模型如下

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

和数据库上下文

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

}
}

然后我尝试启用迁移并通过

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 是如何构建的

enter image description here

在 sessiontest.cs 中,我为 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();

}

我编写单元测试的 ComponentsTest 项目,app.config 如下所示:

<?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>

在库(组件)本身的 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>

最佳答案

其他答案表明这是您的启动项目的问题。

由于您的项目是一个库,您可以尝试根据 this answer 将您的单元测试项目设置为您的启动项目.

您还可以尝试根据已接受的答案将包管理器控制台中的默认项目设置为您的库项目 here .

您可能会遇到创建迁移的问题。如果您需要进一步控制,在 EF6 中有各种参数可以与 Enable-Migrations 一起使用,详见 this answer。但我在这方面没有足够的知识来进一步指导你。您可能需要阅读一些内容。

关于c# - 使用 "SetData"参数调用 "2"的启用迁移异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38436256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com