迁移命令后未创建表

时间:2020-03-15 05:41:00

标签: c# .net entity-framework

我在项目中首先使用实体​​框架6.4.0代码。

这是上下文类:

class LibraryContext : DbContext
{
    public LibraryContext() : base("LibraryContext")
    {}
    public DbSet<Person> persons { get; set; }
    public DbSet<Book> books { get; set; }
    public DbSet<Review> reviews { get; set; }
}

这是应用程序文件中的连接字符串:

<add name="LibraryContext" connectionString="Data Source=DESKTOP-ND0H3GG\LOCALHOST;Initial Catalog=LibraryDB;User ID=sa;Password=blabla;"/>

我在名为“ DESKTOP-ND0H3GG \ LOCALHOST”的服务器中手动创建了一个名为LibraryDB的数据库。

当我在添加迁移和更新数据库命令之后运行enable-migrations时,我希望看到三个表Persons,Books和Reviews。

但是我在数据库中看不到这三个表。 取而代之的是,我看到创建了名为LibraryContext的数据库以及上面提到的三个表 在服务器(localdb)\ MSSQLLocalDB中。

这是它的外观:

enter image description here

知道为什么不在LibraryDB中创建表吗?

1 个答案:

答案 0 :(得分:0)

在创建DbContext时,即在我通过自己的DbContext扩展名创建的NET Core中,应指定connStr:

public static class ServiceCollectionExtensions
{
    public static IServiceCollection ConfigureSqlServerDbContext<TContext>(this IServiceCollection serviceCollection,  string connectionString) 
        where TContext: DbContext
    {
        serviceCollection.AddDbContext<TContext>(options => options.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll)
                                                                   .UseSqlServer(connectionString)
                                                                   .UseLazyLoadingProxies());
        return serviceCollection;
    }
}

并且我正在应用程序启动时运行迁移(在我的示例中,我正在将appsettings.json用于读取配置节,FeesCalculationModelContext是我的Db上下文):

        IList<IConfigurationSection> connections = _configuration.GetSection(ConnectionStringsSectionKey).GetChildren().ToList();
        string feesCalculationConnectionString = connections.First(item => string.Equals(item.Key.ToLower(), FeesCalculationConnectionStringKey.ToLower())).Value;
        FeesCalculationConnectionString = feesCalculationConnectionString;

        // 2. Configure context's
        services.AddEntityFrameworkSqlServer();
        // 2. b. Fees calc
        services.ConfigureSqlServerDbContext<FeesCalculationModelContext>(feesCalculationConnectionString);
        // migrate fees calculation ...
        IServiceProvider serviceProvider = services.BuildServiceProvider();
        FeesCalculationModelContext feesCalculationDbService = serviceProvider.GetService<FeesCalculationModelContext>();
        feesCalculationDbService.Database.Migrate();

如果要指定db并使用命令对其进行更新,则应具有设计时db上下文工厂:

internal class FeesCalcMigrationsDbContextFactory : IDesignTimeDbContextFactory<FeesCalculationModelContext>
{
    public FeesCalculationModelContext CreateDbContext(string[] args)
    {
        //todo: umv: move hardcoded conn string to migrationsSettings.json
        string startupPath = Path.Combine(Environment.CurrentDirectory, @"..\B2b.Data");
        string migrationsConfigFile = Path.Combine(startupPath, "migrations.Development.settings.json");
        IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddJsonFile(migrationsConfigFile, false, false);
        IConfigurationRoot configration = configurationBuilder.Build();
        string connectionString = configration.GetConnectionString("FeesCalculationConnectionString");
        DbContextOptionsBuilder<FeesCalculationModelContext> optionsBuilder = new DbContextOptionsBuilder<FeesCalculationModelContext>();
        optionsBuilder.UseSqlServer(connectionString);
        return new FeesCalculationModelContext(optionsBuilder.Options);
    }
}

对于添加迁移和数据库更新,您应该使用以下命令(NuGet控制台,选择包含您的DbContext的项目):

添加迁移{name} -Context FeesCalculationModelContext -OutputDir FeesCalculation \ Migrations

更新数据库-Context FeesCalculationModelContext