如何允许迁移控制台应用程序?

时间:2018-01-21 02:42:21

标签: c# console-application entity-framework-core

使用asp.net核心和ef核心时,我在调用add-migration init时没有问题。但是当我在下面的控制台应用程序上应用相同的方法时,我收到一个错误说:

  

无法创建“StudentContext”类型的对象。将“IDesignTimeDbContextFactory”的实现添加到项目中,或者参阅https://go.microsoft.com/fwlink/?linkid=851728了解设计时支持的其他模式。

解决此问题的最简单方法是什么?

.net核心控制台应用程序项目如下:

appsettings.json

{
  "ConnectionStrings": {
    "Storage": "Data Source=storage.db"
  }
}

EFCore.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
  </ItemGroup>

</Project>

Student.cs

namespace EFCore.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

StudentContext.cs

using Microsoft.EntityFrameworkCore;

namespace EFCore.Models
{
    public class StudentContext : DbContext
    {
        public StudentContext(DbContextOptions<StudentContext> options) : base(options) { }

        public DbSet<Student> Students { get; set; }
    }
}

Program.cs的

using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace EFCore
{
    class Program
    {
        static void Main(string[] args)
        {
            var configurationBuilder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = configurationBuilder.Build();
            string connectionString = configuration.GetConnectionString("Storage");

            DbContextOptionsBuilder<StudentContext> optionsBuilder = new DbContextOptionsBuilder<StudentContext>()
                .UseSqlite(connectionString);

            using (StudentContext sc = new StudentContext(optionsBuilder.Options))
            {

                sc.Database.Migrate();

                sc.Students.AddRange
                (
                    new Student { Name = "Isaac Newton" },
                    new Student { Name = "C.F. Gauss" },
                    new Student { Name = "Albert Einstein" }
                );

                sc.SaveChanges();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

经过几个小时的努力,我发现解决方案如下:

using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace EFCore
{
    class Program : IDesignTimeDbContextFactory<StudentContext>
    {
        public StudentContext CreateDbContext(string[] args)
        {
            var configurationBuilder = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = configurationBuilder.Build();
            string connectionString = configuration.GetConnectionString("Storage");

            DbContextOptionsBuilder<StudentContext> optionsBuilder = new DbContextOptionsBuilder<StudentContext>()
                .UseSqlite(connectionString);

            return new StudentContext(optionsBuilder.Options);
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            using (StudentContext sc = p.CreateDbContext(null))
            {
                sc.Database.Migrate();

                sc.Students.AddRange
                (
                    new Student { Name = "Isaac Newton" },
                    new Student { Name = "C.F. Gauss" },
                    new Student { Name = "Albert Einstein" }
                );

                sc.SaveChanges();
            }
        }
    }
}

我希望它对其他人也有用!