ASP.NET:如何避免重复种子数据?

时间:2017-03-08 15:12:30

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

当我在启用自动迁移时,使用在迁移文件夹下创建的Configuration.cs文件中的种子方法开发应用程序时播种数据。问题是当我使用" update-database"更改或添加新模型后的命令,它重新播种数据,在任何地方添加双重条目。然后我必须手动删除所有表中的所有内容。我的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();
        }
    }
}

3 个答案:

答案 0 :(得分:3)

context.people.AddOrUpdate()

答案 1 :(得分:2)

只需在Seed方法中添加:

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

答案 2 :(得分:1)

AddOrUpdate()方法仅用于此用途。

如果不是所有属性都在Seed方法中明确说明,您仍然可能会遇到问题。例如,如果您要在构造函数中指定GUID字段或随机数。

如果并非所有属性都匹配,AddOrUpdate()将检查模型中的所有属性并在数据库中创建新条目。