我一直在使用迁移已经有一段时间了,我最终得到了一堆迁移文件。当我只想创建数据一次时,我一直遇到我的Seed方法复制数据(当尝试将Id用作“标识符”)时的麻烦。
现在我正在考虑删除所有迁移文件并重新创建初始文件以稍微整理一下。我还计划使用SQL()在Up()方法中播种数据,而不是使用Seed()方法。
我已经为付费客户运行了一堆网站。我不想冒险结束我必须选择无法更新数据库模式,并且不得不删除客户端数据(这对我来说看起来非常糟糕)。
我对这整个迁移事情感到不确定。我已经在开发的早期阶段出现了迁移被搞砸了,我不得不放弃/重新创建数据库。
所以我的问题归结为......如果删除迁移文件并重新创建一个大的初始迁移,我是否会在更新已运行的网站时遇到问题?
这是我目前的Configuration.cs。我正在使用Web部署中的选项在Application_Start()上执行“执行代码迁移”:
internal sealed class Configuration : DbMigrationsConfiguration<BandPage.Models.BandPageContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(BandPage.Models.BandPageContext context)
{
List<SiteSettings> siteSettings = new List<SiteSettings>
{
new SiteSettings
{
Title = "Page title",
MetaKeywords = "",
MetaDescription = "",
MetaLanguage = "en",
Favicon = "",
FooterText = "",
BackgroundImage = "",
HeaderImage = "",
FooterImage = "",
BackgroundColor = "255,255,255",
ContainerColor = "0,0,0",
ContainerOpacity = 7,
HeadingColor = "0,0,0",
TextColor = "0,0,0",
LinkColor = "0,0,255",
HeadingFont = "Verdana",
HeadingSize = "8",
TextFont = "Verdana",
TextSize = "6",
ContainerWidth = 800,
CustomCSS = ""
}
};
siteSettings.ForEach(s => context.SiteSettings.AddOrUpdate(i => i.SiteSettingsId, s));
context.SaveChanges();
// add fonts to database
List<Font> fonts = new List<Font>
{
new Font { Name = "Impact", FontFamily = "Impact, Charcoal, sans-serif" },
new Font { Name = "Comic Sans", FontFamily = "'Comic Sans MS', cursive, sans-serif" },
new Font { Name = "Palatino", FontFamily = "'Palatino Linotype', 'Book Antiqua', Palatino, serif" },
new Font { Name = "Tahoma", FontFamily = "Tahoma, Geneva, sans-serif" },
new Font { Name = "Century Gothic", FontFamily = "Century Gothic, sans-serif" },
new Font { Name = "Lucida Sans", FontFamily = "'Lucida Sans Unicode', 'Lucida Grande', sans-serif" },
new Font { Name = "Arial Black", FontFamily = "'Arial Black', Gadget, sans-serif" },
new Font { Name = "Times New Roman", FontFamily = "'Times New Roman', Times, serif" },
new Font { Name = "Arial Narrow", FontFamily = "'Arial Narrow', sans-serif" },
new Font { Name = "Verdana", FontFamily = "Verdana, Geneva, sans-serif" },
new Font { Name = "Cooperplate Gothic", FontFamily = "Copperplate, 'Copperplate Gothic Light', sans-serif" },
new Font { Name = "Lucida Console", FontFamily = "'Lucida Console', Monaco, monospace" },
new Font { Name = "Gill Sans", FontFamily = "'Gill Sans', 'Gill Sans MT', sans-serif" },
new Font { Name = "Trebuchet MS", FontFamily = "'Trebuchet MS', Helvetica, sans-serif" },
new Font { Name = "Courier New", FontFamily = "'Courier New', Courier, monospace" },
new Font { Name = "Arial", FontFamily = "Arial, Helvetica, sans-serif" },
new Font { Name = "Georgia", FontFamily = "Georgia, Serif" },
new Font { Name = "Helvetica", FontFamily = "'Helvetica Neue', 'Lucida Grande', Helvetica, Arial, Verdana, sans-serif" }
};
fonts.ForEach(s => context.Fonts.AddOrUpdate(i => i.Name, s));
context.SaveChanges();
// add fixed pages like "Home", "Contact" etc
List<MenuItemPage> menuItemPages = new List<MenuItemPage>
{
new MenuItemPage { PageName = "", PageId = 0, Slug = "" },
new MenuItemPage { PageName = "Blog", PageId = 0, Slug = "blog" },
new MenuItemPage { PageName = "Home", PageId = 0, Slug = "" },
new MenuItemPage { PageName = "Contact", PageId = 0, Slug = "contact" },
new MenuItemPage { PageName = "Shows", PageId = 0, Slug = "tour" },
new MenuItemPage { PageName = "Store", PageId = 0, Slug = "store" },
new MenuItemPage { PageName = "Image gallery", PageId = 0, Slug = "gallery" },
};
menuItemPages.ForEach(s => context.MenuItemPages.AddOrUpdate(i => i.PageName, s));
context.SaveChanges();
}
}
为什么我最终会有多个SiteSettings记录?其他的工作正常。使用Id作为标识符时,我无法使其工作。我只是不想在SiteSettings表中创建一行(对于一些初始值),而不是再创建任何行。
这些网站是一个CMS系统,每个网站都有自己的SQL CE数据库。
希望你能抽出时间帮助我!
/的Mikael
答案 0 :(得分:0)
实体框架应该检查数据库中是否已存在SiteSettingsId。如果它是数据库生成的,那么实体框架将检查是否存在零,如果它没有继续并插入数据,那么它将始终插入。
其他AddOrUpdates可以正常工作,因为您指定了一个非数据库生成的自然键。您可以向SiteSettings类添加自然键