类库中的实体框架6.2不起作用

时间:2018-03-06 18:58:36

标签: c# .net asp.net-mvc entity-framework dll

我有以下问题:我尝试将我的DbContext类放入一个单独的类库中,但它不起作用。

我收到以下错误:

  

异常调用" SetData"用" 2"参数:"键入' Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly' Microsoft.VisualStudio.ProjectSystem.VS.Implementation,Version = 15.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'未标记为可序列化。"
  在D:\ Projekte \ Seminar_Tool_Online \ Seminar_Tool_Online \ packages \ EntityFramework.6.2.0 \ tools \ EntityFramework.psm1:720 char:5
  + $ domain.SetData(' project',$ project)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   + CategoryInfo:NotSpecified:(:) [],MethodInvocationException
  + FullyQualifiedErrorId:SerializationException

     

异常调用" SetData"用" 2"参数:"键入' Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly' Microsoft.VisualStudio.ProjectSystem.VS.Implementation,Version = 15.0.0.0,Culture = neutral,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:NotSpecified:(:) [],MethodInvocationException
  + FullyQualifiedErrorId:SerializationException

     

System.NullReferenceException:未将对象引用设置为对象的实例   at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetPropertyValue [T](Project project,String 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(Action命令)
  对象引用未设置为对象的实例。

我有一个名为" Seminar_Tool_Online"的ASP.NET MVC 5项目。这是我的启动项目。在配置文件中是Entity Framework所需的一切。

<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类:

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

所以我在Package Manager控制台中使用以下命令尝试了它:

Enable-Migrations -EnableAutomaticMigrations -ProjectName Seminar_Tool_Datenbank -StartupProjectName Seminar_Tool_Online

我已经尝试过这里的解决方案:

对我来说没有任何作用,我仍然会得到同样的错误。在这两个项目中,我都使用EF 6.2和.Net 4.5.1

有人可以帮助我吗?

更新

这可能是问题吗?

Make EF NuGet package / migrations and EF Tools work with new csproj format

1 个答案:

答案 0 :(得分:0)

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