c# - ASP.NET : How do I avoid duplicating Seed Data?

标签 c# entity-framework asp.net-core-mvc

我正在播种数据,同时我在启用自动迁移时使用迁移文件夹下创建的 Configuration.cs 文件中的种子方法开发我的应用程序。问题是,当我在更改或添加新模型后使用“更新数据库”命令时,它会重新播种数据,在各处添加双条目。然后我必须手动删除所有表中的所有内容。我的 SQL 服务器是一个单独的 Azure 实例。

如何避免重复?

namespace ChangeApp.Migrations
{
    using Microsoft.AspNet.Identity.EntityFramework;
    using Models;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<ChangeIT.Models.ChangeContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(ChangeIT.Models.ChangeContext context)
        {
            var people = new List<Person>
            {
                new Person { FirstName = "James", LastName = "Monroe", email = "david.bernstein@bdpint.com", manager = false, admin = false },
                new Person { FirstName = "Millard", LastName = "Fillmore", email = "dennis.yu@bdpint.com", manager = false, admin = false },
                new Person { FirstName = "John", LastName = "Adams", email = "jason.bullock@bdpint.com", manager = true, admin = false }
            };
            people.ForEach(s => context.people.Add(s));

            context.SaveChanges();

            var managers = new List<Manager>
            {
                new Manager { EngineerId = 3 }
            };

            managers.ForEach(s => context.managers.Add(s));
            context.SaveChanges();

            using (var db = new ApplicationDbContext())
            {
                if (db.Roles.Count() == 0)
                {
                    var roles = new List<IdentityRole>
                {
                    new IdentityRole("Admin"),
                    new IdentityRole("Manager"),
                    new IdentityRole("User")
                };
                    roles.ForEach(s => db.Roles.Add(s));
                    db.SaveChanges();
                }

            }


            var statuses = new List<Status>
            {
                new Status { StatusName = "Approved" },
                new Status { StatusName = "Pending Approval" },
                new Status { StatusName = "Completed" },
                new Status { StatusName = "Denied" }
            };

            statuses.ForEach(s => context.statuses.Add(s));
            context.SaveChanges();

            var systems = new List<SystemDetail>
            {
                new SystemDetail { SystemName = "Citrix" },
                new SystemDetail { SystemName = "VMWare" },
                new SystemDetail { SystemName = "SQL" }
            };

            systems.ForEach(s => context.systems.Add(s));
            context.SaveChanges();

            var changes = new List<Change>
            {
                new Change { EngineerId = 1, ManagerId = 3, ChangeDescription = "Update Chrome to version 52", ChangeDate = DateTime.Parse("2016-08-19 "), ChangeTime = DateTime.Parse("16:20"), SystemDetailId = 1, StatusId = 1 },
                new Change { EngineerId = 2, ManagerId = 4, ChangeDescription = "Put Cluster 1 blade 36 in maintenance mode. Replace memory in slot 2", ChangeDate = DateTime.Parse("2016-08-26"), ChangeTime = DateTime.Parse("16:20"),SystemDetailId = 2, StatusId = 2 }
            };

            changes.ForEach(s => context.changes.Add(s));
            context.SaveChanges();
        }
    }
}

最佳答案

只需在 Seed 方法的开头添加:

 if (context.people.Any())
            {
                return;   // DB has been seeded
            }

关于c# - ASP.NET : How do I avoid duplicating Seed Data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42674998/

相关文章:

c# - Azure Active Directory 跟踪作者和编辑者字段

c# - 如何在方法的 XML 文档中引用其他类型的成员?

c# - 在 UserControl 中捕获 Esc 键

c# - 如何更新 ObservableCollection 的现有元素?

c# - Entity Framework SaveChanges 被调用但它不工作

c# - 验证 ORA-01438 : value larger than specified precision allowed for this column

c# - 无法从程序集中加载类型 'ADODB.FieldsToInternalFieldsMarshaler'

c# - 使用 SQL_VARIANT 的 Entity Framework

c# - ASP.NET 5/MVC 6 本地事件目录

c# - 如何使用 ASP.NET Core 项目在 Visual Studio 2017 中创建指向图像的符号链接(symbolic link)?