Entity Framework Core:在 WebAPI 中包含多对多相关对象 [英] Entity Framework Core: Include many-to-many related objects in WebAPI

查看:17
本文介绍了Entity Framework Core:在 WebAPI 中包含多对多相关对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太熟悉 .NET 框架,但决定尝试 ASP.NET Core 和 EF Core.我想制作一个非常简单的 Web API 后端,但在处理多对多关系时遇到问题.

I'm not too familiar with the .NET framework but decided to try out ASP.NET Core and EF Core. I want to make a pretty simple Web API backend but I'm having trouble working with many-to-many relationships.

我知道我需要为两个实体制作一个关系表,如这篇文章中的示例所示:如何与最新的 EF Core 每晚构建建立多对多关系?

I understand that I need to make a relationship table for the two entities, as in the example from this post: How to create a many to many relationship with latest nightly builds of EF Core?

我也有我的模型构建器创建这里描述的关系 http://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many

I also have my model builder creating the relationship as described here http://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many

我的代码如下:

在 DBContext 中:

in 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 属性,但它始终为空,尽管连接表有条目.

This seems to be working for the most part. I can create new MovieActor objects and add them to the database. One thing that seems odd is the Movie and Actor models now have a MovieActors property but it's always null, despite the join table having entries.

我想不通的是如何在我的 WebAPI 控制器中检索这些相关对象.我的 Linq 技能很弱,所以也许我只需要复习一下,但我不知道从哪里开始.当我通过 ID 获取电影或获取所有电影的列表时,我希望结果在对象的数组中包含任何相关的 Actor.另一个方向也是如此,当我得到一个演员时,我想得到他们电影的数组.

The thing I can't figure out is how to retrieve these related objects in my WebAPI controllers. My Linq skills are very weak, so maybe I just need to brush up on that, but I don't know where to start. When I get a movie by ID, or get a list of all movies, I'd like the result to have any related Actors in an array on the object. The same goes in the other direction, when getting an Actor I want to get an array of their movies.

这是我试图利用这种关系的控制器方法之一:

Here is one of my controller methods trying to make use of this relationship:

        // 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);
    }

推荐答案

你必须按如下所示进行更改.

You have to change it as shown below.

模型:

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();

这篇关于Entity Framework Core:在 WebAPI 中包含多对多相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆