0

我已经使用迁移有一段时间了,我最终得到了一堆迁移文件。当我只想创建一次数据时,我一直遇到我的 Seed 方法复制数据的麻烦(尝试使用 Id 作为“标识符”时)。

现在我正在考虑删除所有迁移文件并重新创建初始文件以进行整理。我还计划使用 SQL() 在 Up() 方法中播种数据,而不是使用 Seed() 方法。

我已经为付费客户运行了很多网站。我不想冒险最终陷入不得不选择无法更新数据库模式和不得不删除客户端数据的情况(这对我来说看起来真的很糟糕)。

我对整个迁移过程感到不确定。我在开发的早期阶段发生过迁移被搞砸的情况,我不得不删除/重新创建数据库。

所以我的问题归结为......如果我删除迁移文件并重新创建一个大的初始迁移,我会在更新我已经运行的站点时遇到问题吗?

这是我当前的 Configuration.cs。我正在使用 Web Deploy 中的选项来在 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 数据库。

希望你能花时间帮助我!

/迈克尔

4

1 回答 1

0

Entity framework should be checking whether SiteSettingsId already exists in the database. If it is database generated then entity framework will check whether zero exists and if it doesn't go ahead and insert the data, so it will always insert.

The other AddOrUpdates work because you have specified a natural key that is not database generated. You could add a natural key to the SiteSettings class

于 2013-08-20T16:18:00.560 回答