我不太熟悉。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属性,但它总是空的,尽管连接表有条目。
我不能弄清楚的事情是如何检索这些相关的对象在我的WebAPI控制器。我的Linq技能很弱,所以也许我只是需要温习一下,但我不知道从哪里开始。当我通过ID获取电影或获取所有电影的列表时,我希望结果在对象上的数组中有任何相关的actor。在另一个方向上也是一样的,当获得一个演员时,我想获得一个他们的电影数组。
这是我的一个控制器方法试图利用这种关系:
// 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();