gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:18:03 24 4
gpt4 key购买 nike

我正在播种数据,同时我在启用自动迁移时使用迁移文件夹下创建的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com