代码之家  ›  专栏  ›  技术社区  ›  Michael

迁移命令后未创建的表

  •  0
  • Michael  · 技术社区  · 6 年前

    我在项目中首先使用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的数据库。

    当我在“添加迁移”和“更新数据库”命令之后运行“启用迁移”时,我希望看到三个表:Person、Books和Reviews。

    但我在数据库中没有看到这三个表。 相反,我创建了名为LibraryContext的数据库和上述三个表 在服务器(localdb)中\MSSQLLocalDB。

    下面是它的外观:

    enter image description here

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

    0 回复  |  直到 6 年前
        1
  •  0
  •   Michael Ushakov    6 年前

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

    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是我的数据库上下文):

            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 Console,选择包含DbContext的项目):

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

    更新数据库-上下文FeesCalculationModelContext