简体   繁体   中英

Add a graph of entities in Entity Framework Core

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM