实体框架7类库 - 种子/自动迁移

时间:2016-05-02 18:43:55

标签: c# entity-framework entity-framework-core seeding

我有一个带有API的ASP.NET 5应用程序和一个带有Entity Framework 7的独立类库。
我遇到了一些试图让播种和自动迁移工作的问题。我在EntityFramework类库中有一个DbInitializer类,它由API项目中的启动类调用。
API项目中的启动类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Data.Entity;
using MyApp.Data.EntityFramework.DataContext;
using MyApp.Data.EntityFramework;

namespace MyApp.API
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json");

            if (env.IsEnvironment("Development"))
            {
                // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
                builder.AddApplicationInsightsSettings(developerMode: true);
            }

            builder.AddEnvironmentVariables();
            Configuration = builder.Build().ReloadOnChanged("appsettings.json");
        }

        public IConfigurationRoot Configuration { get; set; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<MyAppDataContext>(options =>
                    options.UseSqlServer(Configuration["Data:MyAppConnection:ConnectionString"]));

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseIISPlatformHandler();

            app.UseApplicationInsightsRequestTelemetry();

            app.UseApplicationInsightsExceptionTelemetry();

            app.UseStaticFiles();

            app.UseMvc();

            DbInitializer.Initialize(app.ApplicationServices);
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

Entity Framework类库中的DbInitializer类:

using MyApp.Data.EntityFramework.DataContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Data.Entity;
using MyApp.Domain.Model.Leaflets;

namespace MyApp.Data.EntityFramework
{
    public static class DbInitializer
    {
        private static MyAppDataContext context;
        public static void Initialize(IServiceProvider serviceProvider)
        {
            context = (MyAppDataContext)serviceProvider.GetService<MyAppDataContext>();

            context.Database.Migrate();

            InitializeLeaflets();
        }

        private static void InitializeLeaflets()
        {
            if (!context.Leaflets.Any())
            {
                var leaflets = new List<Leaflet>
                {
                    new Leaflet { Title="Leaflet 1" },
                    new Leaflet { Title="Leaflet 2" },
                    new Leaflet { Title="Leaflet 3" },
                    new Leaflet { Title="Leaflet 4" },
                };

                context.Leaflets.AddRange(leaflets);

                context.SaveChanges();
            }
        }
    }
}

关注这篇文章(EntityFramework 7 (EF7) Migrations. DbContext and StartUp Project are in different assemblies)我正在使用以下命令运行种子和数据库更新:

cd MyApp.API
dnx ef migrations add Initial -p MyApp.Data.EntityFramework
dnx ef database update  -p MyApp.Data.EntityFramework  

这会创建数据库和必要的表,但它不会进行种子设定(我仍然需要迁移,只是运行数据库更新不起作用。)
有没有办法设置种子和自动迁移?

0 个答案:

没有答案