【问题标题】:seeded data is duplicated code first migration种子数据是重复的代码先迁移
【发布时间】:2017-03-27 14:02:26
【问题描述】:

我已经使用代码优先迁移为数据库播种,但是当我在 index.html 中查看播种数据时,我注意到数据被复制了。

这是我植入数据时的配置文件:

内部密封类配置:DbMigrationsConfiguration { 公共配置() { AutomaticMigrationsEnabled = 假; }

    protected override void Seed(OnlineBookStore.Models.OnlineBookStoreDB context)
    {

        var books = new System.Collections.Generic.List<Book>
        {
            new Book {

        BookStatus = new BookStatus { Status = "New" },
            Genre = new Genre { Name = "Thriller" },
            Author = new Author { Name = "Paula Hawkins" },
            Title = "The Girl On The Train",
            Description = "Rachel catches the same commuter train            morning. ",
           ISBN = 0552779776,

            },


        new Book
        {
            BookStatus = new BookStatus { Status = "Best Seller" },
            Genre = new Genre { Name = "Childrens" },
            Author = new Author { Name = "Roald Dahl" },
            Title = "The Witches",
            Description = "Beware. Real witches dress in ordinary clothes",


            ISBN = 0141365471,

        },
         },

       };

 books.ForEach(s =>context.Books.AddOrUpdate(p => new { p.ISBN, p.Title } ));
          context.SaveChanges();


    }
}

}

我真的不确定我是不是错了,花了好几天的时间! 真的很感谢任何人的帮助!谢谢!

【问题讨论】:

    标签: asp.net-mvc entity-framework-migrations


    【解决方案1】:

    您需要在 AddOrUpdate 中指定键以防止重复,因为 Seed() 在每次发布更新数据库时都会运行。

    // build your books collection
        var books = new []
        {
            new Book {
    
        BookStatus = new BookStatus { Status = "New" },
            Genre = new Genre { Name = "Thriller" },
            Author = new Author { Name = "Paula Hawkins" },
            Title = "The Girl On The Train",
            Description = "Rachel catches the same commuter train            morning. ",
           ISBN = 0552779776,
    
            },
    
    
        new Book
        {
            BookStatus = new BookStatus { Status = "Best Seller" },
            Genre = new Genre { Name = "Childrens" },
            Author = new Author { Name = "Roald Dahl" },
            Title = "The Witches",
            Description = "Beware. Real witches dress in ordinary clothes",
    
    
            ISBN = 0141365471,
    
        },
         },
    
       };    
    
    
    context.Books.AddOrUpdate(p => new { p.ISBN, p.Title }, books);
    context.SaveChanges();
    

    http://thedatafarm.com/data-access/take-care-with-ef-4-3-addorupdate-method/

    【讨论】:

    • 谢谢,我这样做了,但它指出 p 在当前上下文中不存在,以下代码中的 p 是我认为错误是原样(p)突出显示红色:新{ p.ISBN, p.Title }
    • 抱歉,无意中复制了代码。如果您已经建立了一个集合,则不需要 foreach。见编辑。
    • 谢谢,试过你的代码还是不行,下面代码中的书籍显示为错误,context.Books.AddOrUpdate(p => new { p.ISBN, p.Title }, books) ;
    • 错误是什么?尝试将列表更改为数组。见编辑。
    • 所以发生的情况是,每次我更新数据库时,都会将种子数据/书籍详细信息添加到数据库中,并且不会删除旧数据。所以我最终得到了重复数据
    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2015-06-26
    • 2011-09-04
    相关资源
    最近更新 更多