首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >删除迁移文件并重新创建Initial.cs

删除迁移文件并重新创建Initial.cs
EN

Stack Overflow用户
提问于 2013-08-20 07:15:42
回答 1查看 2.2K关注 0票数 0

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

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

我已经有一堆网站在为付费客户而运行。我不想冒这样的风险:我必须在不能更新数据库模式和必须删除客户端数据之间做出选择(这对我来说看起来非常糟糕)。

我对迁移这件事感到不安,但不确定。在开发的早期阶段,我遇到过迁移搞砸的情况,我不得不删除/重新创建数据库。

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

这是我当前的Configuration.cs。我使用Web Deploy中的选项在Application_Start()上“执行代码迁移”:

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

发布于 2013-08-21 00:18:01

实体框架应该检查数据库中是否已经存在SiteSettingsId。如果它是数据库生成的,那么实体框架将检查是否存在零,如果它不继续并插入数据,则它将始终插入。

另一个AddOrUpdates之所以有效,是因为您指定了一个不是数据库生成的自然键。您可以向SiteSettings类添加一个自然键

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18324598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档