如何避免使用实体框架使用种子方法生成重复数据

时间:2015-02-24 09:19:33

标签: c# entity-framework

我有一个种子方法,用一些种子数据填满我的数据库。它在很大程度上起作用,但我的导演是重复的。当然这不是我想要的。这是代码:

protected override void Seed(MovieModel.MovieContext context)
    {
        var genres = new List<Genre>
        {
            new Genre{Description = "Action", Movies = new List<Movie>()
            {
                new Movie{Title = "The Expandables", Duration = 120, Director = new Director
                {
                    FirstName = "Silvester", LastName = "Stallone"
                }},
                new Movie{Title = "The Expandables II", Duration = 96, Director = new Director
                {
                    FirstName = "Silvester", LastName = "Stallone"
                }},
                new Movie{Title = "The Expandables III", Duration = 86, Director = new Director
                {
                    FirstName = "Silvester", LastName = "Stallone"
                }}
            }},
            new Genre{Description = "Thriller", Movies = new List<Movie>()
            {
                new Movie{Title = "Pulp Fiction", Duration = 110, Director = new Director
                {
                    FirstName = "Quentin", LastName = "Tarrantino"
                }},
                new Movie{Title = "Inglorious Bastards", Duration = 142, Director = new Director
                {
                    FirstName = "Quentin", LastName = "Tarrantino"
                }}
            }},
            new Genre{Description = "Fantasy", Movies = new List<Movie>()
            {
                new Movie{Title = "The Fellowship of the Ring", Duration = 220, Director = new Director
                {
                    FirstName = "Peter", LastName = "Jackson"
                }},
                new Movie{Title = "The Two Towers", Duration = 210, Director = new Director
                {
                    FirstName = "Peter", LastName = "Jackson"
                }},
                new Movie{Title = "The Return of the King", Duration = 230, Director = new Director
                {
                    FirstName = "Peter", LastName = "Jackson"
                }}
            }},
            new Genre{Description = "Horror", Movies = new List<Movie>()
            {
                new Movie{Title = "Texas Chainsaw Massacre", Duration = 86, Director = new Director
                {
                    FirstName = "Wayne", LastName = "Rooney"
                }},
                new Movie{Title = "Annabelle", Duration = 90, Director = new Director
                {
                    FirstName = "Cristiano", LastName = "Ronaldo"
                }}
            }},
            new Genre{Description = "Science Fiction", Movies = new List<Movie>()
            {
                new Movie{Title = "Interstellar", Duration = 245, Director = new Director
                {
                    FirstName = "Gareth", LastName = "Bale"
                }},
                new Movie{Title = "Pandorum", Duration = 90, Director = new Director
                {
                    FirstName = "David", LastName = "Beckham"
                }}
            }}
        };

        genres.ForEach(g => context.Genres.AddOrUpdate(u => u.Description, g));
        context.SaveChanges();
    }

Director,Movie和Genre类具有简单的get和set属性以及导航属性。因为它目前是插入的类型和电影没有重复的数据。但西尔维斯特史泰龙作为导演插入3次。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是因为您正在创建多个实例Director - EF无法知道如果它看到Director的两个实例具有相同的第一个&amp;姓氏它应该只创建一行,所以它创建两行。

猜猜你想做的事情是这样的:

protected override void Seed(MovieModel.MovieContext context)
{
    var stallone = new Director { FirstName = "Silvester", LastName = "Stallone" };
    var tarrantino = new Director { FirstName = "Quentin", LastName = "Tarrantino" };
    var jackson = new Director { FirstName = "Peter", LastName = "Jackson" };
    var rooney = new Director { FirstName = "Wayne", LastName = "Rooney" };
    var ronaldo = new Director { FirstName = "Cristiano", LastName = "Ronaldo" };
    var bale = new Director { FirstName = "Gareth", LastName = "Bale" };
    var beckham = new Director { FirstName = "David", LastName = "Beckham" };

    var genres = new List<Genre>
    {
        new Genre{Description = "Action", Movies = new List<Movie>()
        {
            new Movie{Title = "The Expandables", Duration = 120, Director = },
            new Movie{Title = "The Expandables II", Duration = 96, Director = stallone},
            new Movie{Title = "The Expandables III", Duration = 86, Director = stallone}
        }},
        new Genre{Description = "Thriller", Movies = new List<Movie>()
        {
            new Movie{Title = "Pulp Fiction", Duration = 110, Director = tarrantino},
            new Movie{Title = "Inglorious Bastards", Duration = 142, Director = tarrantino}
        }},
        new Genre{Description = "Fantasy", Movies = new List<Movie>()
        {
            new Movie{Title = "The Fellowship of the Ring", Duration = 220, Director = jackson},
            new Movie{Title = "The Two Towers", Duration = 210, Director = jackson},
            new Movie{Title = "The Return of the King", Duration = 230, Director = jackson}
        }},
        new Genre{Description = "Horror", Movies = new List<Movie>()
        {
            new Movie{Title = "Texas Chainsaw Massacre", Duration = 86, Director = rooney},
            new Movie{Title = "Annabelle", Duration = 90, Director = ronaldo}
        }},
        new Genre{Description = "Science Fiction", Movies = new List<Movie>()
        {
            new Movie{Title = "Interstellar", Duration = 245, Director = bale},
            new Movie{Title = "Pandorum", Duration = 90, Director = beckham}
        }}
    };

    genres.ForEach(g => context.Genres.AddOrUpdate(u => u.Description, g));
    context.SaveChanges();
}

即。只为数据库中的每一行创建一个Director实例。

(如果您在多个类型中使用相同的电影,最终会遇到同样的问题)