2017-02-12 37 views
13

我试图采取吉米博加德的ContosoUniversityCore project如何设置EF6迁移与ASP.NET核心

我想这样做代码首先迁移,但不知道如何正确设置它。 我将Migrator.EF6.Tools添加到我的项目中。

当我运行Enable-迁移 我得到这个错误:

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. 

回答

6

这里真正的问题是,有不同的EF“口味”。只要到EF根文件,看到了区别:

这里是我的食谱使用迁移与EF 6和.NET核心:

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> 

注:版本有改变

2nd.-在项目中像这样创建上下文

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 
} 

3rd.- 地址迁移:从CMD,类型

转到项目文件夹,

dotnet ef migrations add [NameOFYourMigrationGoesHere] -c YourContext 

注意:不要忘了插件创建的文件源控制,这不是自动神奇的

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

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

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

dotnet ef database update --context ConfigurationContext 

我希望它可以帮助你。

胡安

+0

'Microsoft.EntityFrameworkCore.Tools.DotNet'和'OnConfiguring'是EF7功能。您没有提供EF6.1.3的解决方案。 – Shimmy

1

您的EF6项目必须提供一个IDbContextFactory的实现。 EF6命令行工具将查找并使用该实现,以便它们可以实例化上下文。这是一个例子。

public class SchoolContextFactory : IDbContextFactory<SchoolContext> 
    { 
    public SchoolContext Create() 
    { 
     return new EF6.SchoolContext("Server=(localdb)\\mssqllocaldb;   Database=EF6MVCCore;Trusted_Connection=True;MultipleActiveResultSets=true"); 
    } 
    } 

在Core项目的Startup.cs文件中,在ConfigureServices中设置依赖项注入(DI)的EF6上下文。 EF上下文对象应该针对每个请求的生存期进行限定。

public void ConfigureServices(IServiceCollection services) 
{ 
    // Add framework services. 
    services.AddMvc(); 
    services.AddScoped<SchoolContext>(_ => new SchoolContext(Configuration.GetConnectionString("DefaultConnection"))); 
} 

在两个项目的包管理器控制台(PMC)中,运行命令Install-Package Entityframework。

在类库项目,创建数据模型类和上下文类,以及IDbContextFactory的实现。

在PMC的类库项目,运行命令启用的迁移和添加迁移初期。如果您已将ASP.NET Core项目设置为启动项目,请将-StartupProjectName EF6添加到这些命令中。在Startup.cs中的 ,在appsettings.json中注册DI的上下文,添加连接字符串。

欲了解更多信息请访问

https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6

0

的问题是EF 6迁移不习惯新的csproj格式工作。

什么帮助我为this项目。

查看其主页上的说明。

一般来说,你只需要将包添加到您的项目中所指示的方式,然后迁移应该工作。只有