<small id='xRaZZ'></small><noframes id='xRaZZ'>

  1. <legend id='xRaZZ'><style id='xRaZZ'><dir id='xRaZZ'><q id='xRaZZ'></q></dir></style></legend>
  2. <tfoot id='xRaZZ'></tfoot>
        <bdo id='xRaZZ'></bdo><ul id='xRaZZ'></ul>
    1. <i id='xRaZZ'><tr id='xRaZZ'><dt id='xRaZZ'><q id='xRaZZ'><span id='xRaZZ'><b id='xRaZZ'><form id='xRaZZ'><ins id='xRaZZ'></ins><ul id='xRaZZ'></ul><sub id='xRaZZ'></sub></form><legend id='xRaZZ'></legend><bdo id='xRaZZ'><pre id='xRaZZ'><center id='xRaZZ'></center></pre></bdo></b><th id='xRaZZ'></th></span></q></dt></tr></i><div id='xRaZZ'><tfoot id='xRaZZ'></tfoot><dl id='xRaZZ'><fieldset id='xRaZZ'></fieldset></dl></div>

      在 Entity Framework 7 RC 1 和 ASP.NET MVC 6 中播种初始数据

      时间:2023-07-11

      • <tfoot id='gH009'></tfoot>
      • <small id='gH009'></small><noframes id='gH009'>

          <bdo id='gH009'></bdo><ul id='gH009'></ul>

            <legend id='gH009'><style id='gH009'><dir id='gH009'><q id='gH009'></q></dir></style></legend>

                <i id='gH009'><tr id='gH009'><dt id='gH009'><q id='gH009'><span id='gH009'><b id='gH009'><form id='gH009'><ins id='gH009'></ins><ul id='gH009'></ul><sub id='gH009'></sub></form><legend id='gH009'></legend><bdo id='gH009'><pre id='gH009'><center id='gH009'></center></pre></bdo></b><th id='gH009'></th></span></q></dt></tr></i><div id='gH009'><tfoot id='gH009'></tfoot><dl id='gH009'><fieldset id='gH009'></fieldset></dl></div>
                  <tbody id='gH009'></tbody>
                本文介绍了在 Entity Framework 7 RC 1 和 ASP.NET MVC 6 中播种初始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 Entity Framework 7 中似乎还没有对种子数据的原生支持(https://github.com/aspnet/EntityFramework/issues/629).

                It seems that in Entity Framework 7 there is no native support for seed data yet (https://github.com/aspnet/EntityFramework/issues/629).

                微软提供的模板代码中没有DbMigrationsConfiguration类,没有Seed方法.

                There is no DbMigrationsConfiguration class, no Seed method in the template code provided by Microsoft.

                那么如何在使用 Entity Framework 7 RC 1 的 ASP.NET MVC 6 Web 应用程序中播种数据?

                推荐答案

                我为自己找到了一个临时解决方法.

                I've found a temporary workaround for myself.

                我们可以创建一个方法 SeedData 扩展 IApplicationBuilder 然后通过 GetService 方法获取我们的数据库上下文类的实例并将其用于播种数据.

                We can create a method SeedData that extends the IApplicationBuilder then gets an instance of our database context class through GetService method and uses it for seeding the data.

                这是我的扩展方法的样子:

                Here is how my extension method looks like:

                using Microsoft.AspNet.Builder;
                using Microsoft.Extensions.DependencyInjection;
                
                public static class DataSeeder
                {
                    // TODO: Move this code when seed data is implemented in EF 7
                
                    /// <summary>
                    /// This is a workaround for missing seed data functionality in EF 7.0-rc1
                    /// More info: https://github.com/aspnet/EntityFramework/issues/629
                    /// </summary>
                    /// <param name="app">
                    /// An instance that provides the mechanisms to get instance of the database context.
                    /// </param>
                    public static void SeedData(this IApplicationBuilder app)
                    {
                        var db = app.ApplicationServices.GetService<ApplicationDbContext>();
                
                        // TODO: Add seed logic here
                
                        db.SaveChanges();
                    }
                }
                

                要使用它,请将 app.SeedData(); 行放在应用程序 Startup 类的 Configure 方法中(位于 web 项目中在名为 Startup.cs 的文件中).

                To use it put app.SeedData(); line in the Configure method of the application Startup class (located in the web project in file called Startup.cs).

                // 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)
                {
                    app.SeedData();
                
                    // Other configuration code
                }
                

                这篇关于在 Entity Framework 7 RC 1 和 ASP.NET MVC 6 中播种初始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:ASP.NET 5 中 Global.json 中项目部分的说明 下一篇:如何注册自定义 UserStore &amp;DI 中的用户管理器

                相关文章

                • <bdo id='BT5gK'></bdo><ul id='BT5gK'></ul>

                <legend id='BT5gK'><style id='BT5gK'><dir id='BT5gK'><q id='BT5gK'></q></dir></style></legend>

                <small id='BT5gK'></small><noframes id='BT5gK'>

                  <tfoot id='BT5gK'></tfoot>
                  1. <i id='BT5gK'><tr id='BT5gK'><dt id='BT5gK'><q id='BT5gK'><span id='BT5gK'><b id='BT5gK'><form id='BT5gK'><ins id='BT5gK'></ins><ul id='BT5gK'></ul><sub id='BT5gK'></sub></form><legend id='BT5gK'></legend><bdo id='BT5gK'><pre id='BT5gK'><center id='BT5gK'></center></pre></bdo></b><th id='BT5gK'></th></span></q></dt></tr></i><div id='BT5gK'><tfoot id='BT5gK'></tfoot><dl id='BT5gK'><fieldset id='BT5gK'></fieldset></dl></div>