本文介绍了类库中的实体框架6.2无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我遇到以下问题:我试图将DbContext类放入一个单独的类库中,但是它不起作用.

I have the following problem: I tried to put my DbContext class into a separate class library, but it doesn't work.

我收到以下错误:

使用"2"个参数调用"SetData"的异常:在程序集"Microsoft.VisualStudio.ProjectSystem.VS.Implementation,版本"中键入"Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject" = 15.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a'未标记为可序列化."
在D:\ Projekte \ Seminar_Tool_Online \ Seminar_Tool_Online \ packages \ EntityFramework.6.2.0 \ tools \ EntityFramework.psm1:721 char:5
+ $ domain.SetData('contextProject',$ contextProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 〜
+ CategoryInfo:未指定:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:SerializationException

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable."
At D:\Projekte\Seminar_Tool_Online\Seminar_Tool_Online\packages\EntityFramework.6.2.0\tools\EntityFramework.psm1:721 char:5
+ $domain.SetData('contextProject', $contextProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException

System.NullReferenceException:对象引用未设置为对象的实例.
在System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetPropertyValue [T](项目项目,字符串propertyName)上
在System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade处(String configurationTypeName,Boolean useContextWorkingDirectory)
在System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName)处
在System.Data.Entity.Migrations.EnableMigrationsCommand中.<> c__DisplayClass2.< .ctor> b__0()
在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(操作命令)上
对象引用未设置为对象的实例.

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetPropertyValue[T](Project project, String propertyName)
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.

我有一个名为"Seminar_Tool_Online"的ASP.NET MVC 5项目,这是我的启动项目.并在配置文件中提供了实体框架所需的一切.

I have an ASP.NET MVC 5 project with the name "Seminar_Tool_Online" this is my start up project. And in the config file is everything Entity Framework needs.

<configSections>
    <section name="entityFramework"
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false"/>
</configSections>
<connectionStrings>
    <add name="AccountConnection"
         connectionString="Server=...; Database=...;User Id = ...; password=...; Integrated Security=False"
         providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
        <providers>
            <provider invariantName="System.Data.SqlClient"
                      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
        </providers>
    </entityFramework>

我的类库称为Seminar_Tool_Datenbank,具有以下DbContext类:

My class library is called Seminar_Tool_Datenbank with the following DbContext class:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
            public ApplicationDbContext()
                : base("AccountConnection", throwIfV1Schema: false)
            {
            }

            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
            }
    }

所以我在包管理器控制台中使用以下命令尝试了此操作:

So I tried it with the following command in the Package Manager Console:

Enable-Migrations -EnableAutomaticMigrations -ProjectName Seminar_Tool_Datenbank -StartupProjectName Seminar_Tool_Online

我已经从这里尝试了解决方案:

I have already tried the solutions from here:

  • Enable-Migrations Exception calling "SetData" with "2" argument(s)
  • https://www.codeproject.com/Tips/840863/Entity-Framework-Migrations-enabled-in-Data-Access

对我没有任何帮助,我仍然遇到相同的错误.在两个项目中,我都使用EF 6.2和.Net 4.5.1

Nothing works for me, I still get the same error. In both projects, I use EF 6.2 and .Net 4.5.1

有人可以帮助我吗?

更新

这可能是问题吗?

使EF NuGet包/迁移和EF Tools可以使用新的csproj格式

推荐答案

我可以通过将csproj文件更改为VS 2015的旧标准来解决该问题.

I can solve the problem by change the csproj-file to the old standard of VS 2015.

这篇关于类库中的实体框架6.2无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 18:56