我不太熟悉.NET框架,但决定试用ASP.NET Core和EF Core。我想制作一个非常简单的Web API后端,但我在处理多对多关系时遇到了麻烦。
我理解我需要为这两个实体创建一个关系表,如以下帖子中的示例所示: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
我的代码如下所示:
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);
}
答案 0 :(得分:4)
您必须如下所示进行更改。
模特:
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();