2

I have a problem with seeding data into an EF Core DB. The data consists of movies that have a Person object as a relation for their director. The relation works on the basis that a movie should have one director and each director entity in the DB has one directed movie (not ideal but besides the point). The problem arises when I try to seed the initial data in the DB. Since I'm seeding both the movies and the directors, I can't know the IDs of the entities beforehand and I need to be able to search the context for a director with a given name before creating a movie that has a relation to it.

For example:

This is the seeding of the people (directors)

public void Configure(EntityTypeBuilder<Person> builder)
{
    builder.ToTable("Persons");
    builder.Property(p => p.Id).HasColumnName("Id");
    builder.HasData(
        new Person
        {
            Id = Guid.NewGuid().ToString(),
            Name = "Ridley Scott"
        }
    );
}

This is the seeding of the movies

public void Configure(EntityTypeBuilder<Movie> builder)
{
    using AtlasDbContext context = new AtlasDbContext();
    Person director = context.Persons.FirstOrDefault(p => p.Id.Equals("Ridley Scott"));
    builder.ToTable("Movies");
    builder.Property(m => m.Id).HasColumnName("Id");
    builder.HasData(
        new Movie
        {
            Title = "The Martian",
            Poster = File.ReadAllBytes($"{Environment.CurrentDirectory}\\wwwroot\\img\\TheMartian.poster.jpg"),
            Cover = File.ReadAllBytes($"{Environment.CurrentDirectory}\\wwwroot\\img\\TheMartian.cover.jpg"),
            ReleaseYear = "2015",
            SubTitle = "Bring Him Home",
            UserScore = 77,
            Overview = "During a manned mission to Mars," +
                       "Astronaut Mark Watney is presumed dead after a fierce storm and left behind by his crew. " +
                       "But Watney has survived and finds himself stranded and alone on the hostile planet. " +
                       "With only meager supplies, he must draw upon his ingenuity, " +
                       "wit and spirit to subsist and find a way to signal to Earth that he is alive.",
            DirectorId = director?.Id,
            Budget = 108000000,
            Revenue = 630161890,
            Genres = new List<Genre>
            {
                Genre.Adventure,
                Genre.Drama,
                Genre.ScienceFiction
            }
        }
    );
}

The DB context is instantiated without dependency injection because I can't DI the context in an overridden method. However, when I try to create a migration for this seed the migration succeeds the build but doesn't create anything and the terminal isn't available for input (stuck on "Build succeeded") which leads me to believe that an infinite loop is created by this code.

How would one go about seeding data of this type in EF Core?

Emberfire
  • 89
  • 7

1 Answers1

1

I generally don't recommend creating new GUID every time. This code will generate a different ID when deployed to different environments i.e staging and production, so you will only be left to query objects by name which creating ID column seems useless.Also you need to define primary key for each entity. So it will be better if you hardcode your GUID's like

var personId="17FD2873-FF9D-4951-9346-1C71BD0AC0DE";
new Person
    {
        Id = personId,
        Name = "Ridley Scott"
    }
Harkirat singh
  • 599
  • 6
  • 19