我已經使用了一段時間的遷移,並且我已經結束了一堆遷移文件。我一直有麻煩,我的種子方法重複數據(當試圖使用Id作爲「標識符」),當我只想創建一次數據。刪除遷移文件並重新創建Initial.cs
現在我正在考慮刪除所有遷移文件並重新創建一個初始文件以整理一下。我還計劃使用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();
}
}
爲什麼我結束了站點設置的多個記錄?其他人按照他們應該的方式工作。使用Id作爲標識符時,我無法使其正常工作。我只是不想在SiteSettings表中創建一行(對於一些初始值),而不是創建更多行。
這些網站是一個CMS系統,每個網站都有自己的SQL CE數據庫。
希望你能花點時間幫助我!
/的Mikael
所以你會推薦創建一個特定的列專爲此目的?原因我無法正確使用其中一個現有值,因爲所有這些值都可以並且將由用戶更改。 –
如果您停止在數據庫中生成它,則可以使用SiteSettingsID。但請記住,如果您在活動數據庫上運行Seed方法,則AddOrUpdate將更新用戶已更改的值 – Colin
順便說一句,在我自己的數據庫中,我有一個包含鍵和值列的設置表。這意味着我不必每次需要更改設置時都要更改模式 – Colin