【发布时间】:2021-07-24 07:51:36
【问题描述】:
我有三个表“电影”、“流派”,它与表“电影流派”相关联。我要做的是将 Movie 表的值与 Genre 表结合起来,并使用第三个关联表 MovieGenre 显示 Genre 表中的 Name 值和 Movie 表值。
public partial class Movie
{
public int MovieId { get; set; }
[Required]
public string Title { get; set; }
public string Description { get; set; }
public string Storyline { get; set; }
public int? Year { get; set; }
[DataType(DataType.Date)]
[Validators(ErrorMessage = "Date must be after or equal to current date")]
[Display(Name = "Release Date")]
public DateTime? ReleaseDate { get; set; }
public int? Runtime { get; set; }
[Display(Name = "Movie Type")]
[Column(TypeName = "nvarchar(20)")]
public MovieType MovieType { get; set; }
public ICollection<Genre> Genres { get; set; }
}
public class Genre
{
[Key]
public int GenreId { get; set; }
[Display(Name="Genre name")]
public string Name { get; set; }
public ICollection<Movie> Movies { get; set; }
}
public class MovieGenre
{
public int MovieGenreId { get; set; }
public int MovieId { get; set; }
public Movie Movie { get; set; }
public int GenreId { get; set; }
public Genre Genre { get; set; }
}
这是这个的上下文页面
public partial class MovieContext : DbContext
{
public MovieContext(DbContextOptions<MovieContext> options)
: base(options)
{
}
public virtual DbSet<Movie> Movies { get; set; }
public virtual DbSet<Genre> Genre { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
modelBuilder.Entity<MovieGenre>().HasKey(mg => new { mg.MovieId, mg.GenreId });
modelBuilder.Entity<Movie>()
.HasMany(p => p.Genres)
.WithMany(p => p.Movies)
.UsingEntity(j => j.ToTable("MovieGenre"));
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
我使用了数据库优先的方法。我已经创建了所有其他部分,当我从数据库向表中输入值时它工作正常。但我想要实现的是在创建新电影时将外键值“MovieId”和“GenreId”存储到“MovieGenre”表中。
这是创建动作方法中的代码。如何通过此代码将“MovieId”和“GenreId”存储到“MovieGenre”表中?
public async Task<IActionResult> Create([Bind("Title,Description,Storyline,Year,ReleaseDate,Runtime,MovieType")] Movie movies)
{
if (ModelState.IsValid)
{
_context.Add(movies);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewBag.GenreId = new MultiSelectList(_context.Genre.ToList(), "GenreId", "Name");
return View(movies);
}
下面是创建 Action 视图的代码,这里我使用了 Genre 的复选框,我也想从这里以某种方式将 GenreId 输入到关联表中。
<div class="row">
<div class="col-md-4 center">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Storyline" class="control-label"></label>
<input asp-for="Storyline" class="form-control" />
<span asp-validation-for="Storyline" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Year" class="control-label"></label>
<input asp-for="Year" class="form-control" />
<span asp-validation-for="Year" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ReleaseDate" class="control-label"></label>
<input asp-for="ReleaseDate" class="form-control" />
<span asp-validation-for="ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Runtime" class="control-label"></label>
<input asp-for="Runtime" class="form-control" />
<span asp-validation-for="Runtime" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="MovieType" class="control-label"></label>
<select asp-for="MovieType" class="form-control" asp-items="Html.GetEnumSelectList<MovieType>()"></select>
<span asp-validation-for="MovieType" class="text-danger"></span>
</div>
<div class="form-group">
<label class="control-label">Movie Genre</label>
<div class="col-md-10">
<div class="checkbox">
@foreach (var item in (MultiSelectList)ViewBag.GenreId)
{
<input type="checkbox" value="@item.Value" id="GenreId" name="GenreId" />@item.Text
}
</div>
</div>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
<a class="btn btn-primary" asp-action="Index">Back to List</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
【问题讨论】:
标签: asp.net-core-mvc entity-framework-core-5