Add a graph of entities in Entity Framework Core

.net c# entity-framework entity-framework-core

Question

I have used the code-first approach with EF Core 1.1.0 to create my entities in my SQL Database.

Some of my entities have navigation properties to other entities (one-to-one or one-to-many).

The thing is, when I try to add an entity which has navigation properties, the root entity is successfully added, but the related entities are not.

Here is the code below:

public class PopcornContext : DbContext
{
    public PopcornContext(DbContextOptions<PopcornContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Movie> MovieSet { get; set; }
}

public partial class Movie
{
    public Movie()
    {
        this.Genres = new HashSet<Genre>();
        this.Cast = new HashSet<Cast>();
    }

    public int Id { get; set; }
    public string Url { get; set; }
    public string ImdbCode { get; set; }
    public string Title { get; set; }
    public string TitleLong { get; set; }
    public string Slug { get; set; }
    public int Year { get; set; }

    public virtual ICollection<Genre> Genres { get; set; }
    public virtual ICollection<Cast> Cast { get; set; }
}

public class PopcornContextFactory : IDbContextFactory<PopcornContext>
{
    public PopcornContext Create(DbContextFactoryOptions options)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");
        var configuration = builder.Build();

        var optionsBuilder = new DbContextOptionsBuilder<PopcornContext>();
        optionsBuilder.UseSqlServer(configuration["SQL:ConnectionString"]);

        return new PopcornContext(optionsBuilder.Options);
    }
}

When I insert a movie like :

using (var context = new PopcornContextFactory().Create(new DbContextFactoryOptions()))
{
    var movie = new Database.Movie
    {
        ImdbCode = movieJson.ImdbCode,
        TitleLong = movieJson.TitleLong,
        Year = movieJson.Year,
        Cast = movieJson.Cast.Select(cast => new Database.Cast
        {
            ImdbCode = cast?.ImdbCode,
            SmallImage = cast?.SmallImage,
            CharacterName = cast?.CharacterName,
            Name = cast?.Name
        }).ToList(),
        Genres = movieJson.Genres.Select(genre => new Database.Genre
        {
            Name = genre
        }).ToList(),
        Slug = movieJson.Slug,
        Title = movieJson.Title,
        Url = movieJson.Url
    };

    await context.MovieSet.AddAsync(movie);
    await context.SaveChangesAsync();
}

The movie is added into the database with all of its fields, but Genres and Cast are not there (null values).

I have set up my code-first approach with:

Add-Migration Initial

Update-Database

What did I miss? I'm pretty sure this code would work with EF6.

1
2
3/3/2017 3:04:31 PM

Accepted Answer

I've figured it out. Actually it is a known bug with AddAsync method in EF Core. The non async Add method works as expected.

See here

0
3/3/2017 3:53:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow