如何使用ASP.NET Core设置EF6迁移

Pet*_*Mor 14 ef-migrations entity-framework-6 asp.net-core

我正在尝试采用Jimmy Bogard的ContosoUniversityCore项目

我想首先进行代码迁移,但不确定如何正确设置它.我将Migrator.EF6.Tools添加到我的项目中.

当我运行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 C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:718 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=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not 
marked as serializable."
At C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:719 char:5
+     $domain.SetData('contextProject', $contextProject)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + 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=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not 
marked as serializable."
At C:\Users\SomeUser\.nuget\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.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.
Run Code Online (Sandbox Code Playgroud)

Jua*_*uan 5

这里真正的问题是 EF 有不同的“口味”。只需转到 EF 根文档即可查看差异:

以下是我使用 EF 6 和 .NET Core 进行迁移的秘诀:

1st.- 您必须将这些行添加到.csproj

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

注:版本已更改

2.- 在您的项目中创建一个上下文,如下所示:

public class YourContext : DbContext
{
    #region Constructors

    public YourContext()
    {
    }

    public YourContext(DbContextOptions options) : base(options)
    {

    }

    #region DbSets  // YOUR DB SETS GO HERE...
    #endregion DbSets

    #region OnConfiguring // THIS HELPED ME A LOT:
    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        // In order to be able to create migrations and update database:
        if (!options.IsConfigured)
        {
            options.UseSqlServer("YourLocalConnectionStringShouldBeHere");
        }
        base.OnConfiguring(options);
    }
    #endregion

    #region Model Creating

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
       // Your Model Crerating Stuff
    }

    #endregion Model Creating
}
Run Code Online (Sandbox Code Playgroud)

3.-添加迁移

转到项目文件夹,然后从 cmd键入:

dotnet ef migrations add [NameOFYourMigrationGoesHere] -c YourContext
Run Code Online (Sandbox Code Playgroud)

注意:不要忘记将创建的文件添加到源代码管理中,这不是自动完成的

4rd.-更新您的数据库: 4.a.-在 docker 中-> 您可以运行该项目(完全使用 Docker),并且迁移将在第一次使用 Context 时应用。

注意:(它将使用为该环境配置的连接字符串)

4.b.-您的本地数据库-> 编辑在 ConfigurationContext.OnConfigure 中硬编码的连接字符串并运行(从 cmd 控制台):

dotnet ef database update --context ConfigurationContext
Run Code Online (Sandbox Code Playgroud)

我希望它对你有帮助。

胡安

  • “Microsoft.EntityFrameworkCore.Tools.DotNet”和“OnConfiguring”仅是 EF7 功能。您没有提供 EF6.1.3 的解决方案。 (5认同)