Entity Framework 6.2 in Class Library doesn't

2019-06-07 03:52发布

问题:

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

I get the following 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=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:720 char:5
+ $domain.SetData('project', $project)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], 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: 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.

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>

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

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

Can someone help me?

UPDATE

Can this be the problem?

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

回答1:

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