實體框架核心:在WebAPI中包含多對多相關對象

asp.net-core asp.net-core-webapi entity-framework entity-framework-core linq

我不太熟悉.NET框架,但決定試用ASP.NET Core和EF Core。我想製作一個非常簡單的Web API後端,但我在處理多對多關係時遇到了麻煩。

我理解我需要為這兩個實體建立一個關係表,如本文中的示例所示: 如何與最新的EF Core夜間版本建立多對多的關係?

我也有我的模型構建器創建了這裡描述的關係http://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many

我的代碼如下所示:

在DBContext中:

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<MovieActor>().HasKey(x => new { x.MovieId, x.ActorId});

        builder.Entity<MovieActor>()
            .HasOne(m => m.Movie)
            .WithMany(ma => ma.MovieActors)
            .HasForeignKey(m => m.MovieId);

        builder.Entity<MovieActor>()
            .HasOne(m => m.Actor)
            .WithMany(ma => ma.MovieActors)
            .HasForeignKey(a => a.ActorId);

    }

    public DbSet<Movie> Movies { get; set; }

    public DbSet<Actor> Actors { get; set; }


}

楷模:

namespace Test.Models
{
    public class Movie
    {
        public int MovieId { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Code { get; set; }
        public string Path { get; set; }

        public ICollection<MovieActor> MovieActors { get; set; }
    }

    public class Actor
    {
        public int ActorId { get; set; }
        public string Name { get; set; }
        public ICollection<MovieActor> MovieActors { get; set; }
    }

    public class MovieActor
    {
        public int MovieId { get; set; }
        public Movie Movie { get; set; }

        public int ActorId { get; set; }
        public Actor Actor { get; set; }
    }
}

這似乎在很大程度上起作用。我可以創建新的MovieActor對象並將它們添加到數據庫中。有一點似乎很奇怪的是Movie和Actor模型現在有一個MovieActors屬性,但它總是為null,儘管連接表有條目。

我無法弄清楚的是如何在我的WebAPI控制器中檢索這些相關對象。我的Linq技能非常弱,所以也許我只需要了解它,但我不知道從哪裡開始。當我通過ID獲取電影或獲得所有電影的列表時,我希望結果在對像上的數組中包含任何相關的Actors。在另一個方向上也是如此,當得到一個演員我想得到他們的電影陣列。

這是我嘗試使用這種關係的一種控制器方法:

        // POST: api/Movie
    [HttpPost]
    public IActionResult PostMovie([FromBody] MovieBundle Moviebundle)
    {


        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var _movie = _context.Movies.FirstOrDefault(m => m.Code == Moviebundle.Movie.Code);
        if(_movie == null)
        {
            //Doesn't exist! add a new one
            _movie = Moviebundle.Movie;
            _context.Movies.Add(_movie);
            _context.SaveChanges();

            foreach (Actor actor in Moviebundle.actors)
            {
                //Try to look up by name
                Actor _actor = _context.Actors.FirstOrDefault(a => a.Name == actor.Name);

                //If they don't exist, add them to the DB 
                if (_actor == null)
                {
                    _context.Actors.Add(actor);
                    _context.SaveChanges();
                    _actor = actor;
                }

                //This is what I'm using to create the M-t-M relationship
                var link = new MovieActor { Actor = _actor, Movie = _movie };
                _context.MovieActor.Add(link); //This line gives an error


            }


        } else
        {
            return new StatusCodeResult(StatusCodes.Status409Conflict);
        }




        try
        {
            _context.SaveChanges();
        }
        catch (DbUpdateException)
        {
            if (MovieExists(_movie.MovieID))
            {
                return new StatusCodeResult(StatusCodes.Status409Conflict);
            }
            else
            {
                throw;
            }
        }

        return CreatedAtAction("GetMovie", new { id = _movie.MovieID }, _movie);
    }

熱門答案

你必須改變它,如下所示。

楷模 :

namespace Test.Models
{
    public class Movie
    {
        public int MovieId { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Code { get; set; }
        public string Path { get; set; }

        public ICollection<MovieActor> MovieActors { get; set; }
    }

    public class Actor
    {
        public int ActorId { get; set; }
        public string Name { get; set; }
        public ICollection<MovieActor> MovieActors { get; set; }
    }

    public class MovieActor
    {
        public int MovieId { get; set; }
        public Movie Movie { get; set; }

        public int ActorId { get; set; }
        public Actor Actor { get; set; }
    }
}

背景:

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<MovieActor>().HasKey(x => new { x.MovieId, x.ActorId});

        builder.Entity<MovieActor>()
            .HasOne(m => m.Movie)
            .WithMany(ma => ma.MovieActors)
            .HasForeignKey(m => m.MovieId);

        builder.Entity<MovieActor>()
            .HasOne(m => m.Actor)
            .WithMany(ma => ma.MovieActors)
            .HasForeignKey(a => a.ActorId);

    }

    public DbSet<Movie> Movies { get; set; }

    public DbSet<Actor> Actors { get; set; }

    public DbSet<MovieActor> MovieActors { get; set; }

 }

您可以如下所示導航

var actor = context.Actors
                   .Include(r => r.MovieActors)
                   .FirstOrDefault(a=>a.ActorId = 1)

var movies = actor.MovieActors.Select(c => c.Movie);

如何添加記錄:

var actor = new Actor { ... };
context.Actors.Add(actor);

var movie = new Movie { ... };
context.Movies.Add(movie);

var movieActor = new MovieActor { Actor = actor , Movie = movie };
context.MovieActors.Add(movieActor);

context.SaveChanges();


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因