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

时间:2017-02-12 02:33:36

标签: asp.net-core entity-framework-6 ef-migrations

我正在尝试采用Jimmy Bogard的ContosoUniversityCore project

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

运行Enable-Migrations时 我收到这个错误:

function pepareAttachment( $filename ,$fileorgname) {

   $attachContent = '';
   $file = fopen($filename,"rb");
   $data = fread($file,filesize($filename));
   fclose($file);
   $cvData = chunk_split(base64_encode($data));
   $attachContent .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fileorgname\"\n" . 
"Content-Disposition: attachment;\n" . " filename=\"$fileorgname\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . $cvData . "\n\n";
  $attachContent .= "--{$mime_boundary}\n"; 
  return $attachContent;

}

function sendMailAsAttachment( $filename, $fileorgname, $formData ) {

$emailData = prepareEmail( $formData );
$attachContent = prepareAttachment( $filename,$fileorgname );
$message = $emailData['message'].$attachContent;
$ok = @mail($emailData['to'], $emailData['subject'], $message, $emailData['headers']); 
if ($ok) { 
        echo "<p>mail sent to $to!</p>"; 
} else { 
        echo "<p>mail could not be sent!</p>"; 
} 
}

3 个答案:

答案 0 :(得分:4)

这里真正的问题是有不同的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>

注意:版本已更改

第二.-在项目中创建一个上下文,如下所示:

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.- 添加移民

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

dotnet ef migrations add [NameOFYourMigrationGoesHere] -c YourContext

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

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

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

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

dotnet ef database update --context ConfigurationContext

我希望它可以帮到你。

答案 1 :(得分: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中为类库项目运行命令Enable-Migrations和Add-Migration Initial。如果已将ASP.NET Core项目设置为启动项目,请将-StartupProjectName EF6添加到这些命令。 在Startup.cs中,在appsettings.json中注册DI的上下文,添加连接字符串。

欲了解更多信息,请访问

  

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

答案 2 :(得分:0)

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

帮助我的是this项目。

查看主页上的说明。

通常,您只需以指示的方式将包添加到项目中,然后迁移即可。