英文:
EF Core: The entity type 'List<Course>' was not found. Ensure that the entity type has been added to the model
问题
我正在尝试更新数据库中的Courses
表中的列,但遇到以下错误:
未找到实体类型'List
'。请确保已将实体类型添加到模型中。
基本上,我正在尝试获取复选框的状态。例如,如果一位教练被选为课程的作者,然后将教练的Id输入到Course.AuthorId
中。
这是控制器中的方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditInstructorCourse(int id, List<EditInstructorCourseViewModel> model)
{
if (_context.Courses is null)
{
return NotFound();
}
var courses = await _context.Courses.ToListAsync();
try
{
for (int i = 0; i < model.Count; i++)
{
if (model[i].IsAuthor = true)
{
courses[i].AuthorId = id;
_context.Update(courses);
}
else
{
courses[i].AuthorId = null;
}
}
_context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
throw;
}
return RedirectToAction(nameof(Index));
}
这是视图模型:
public class EditInstructorCourseViewModel
{
public string CourseId { get; set; }
public string CourseName { get; set; }
public string CourseAuthor { get; set; }
public string CourseEditor { get; set; }
public bool IsAuthor { get; set; }
public bool IsEditor { get; set; }
}
模型类:
public class Instructor
{
public int Id { get; set; }
[DisplayName("Instructor")]
public string InstructorName { get; set; }
[ForeignKey("AuthorId")]
[DisplayName("Author Of")]
public ICollection<Course> CoursesAuthored { get; set; } = new List<Course>();
[ForeignKey("EditorId")]
[DisplayName("Editor Of")]
public List<Course> CoursesEdited { get; set; } = new List<Course>();
}
public class Course
{
public int Id { get; set; }
[DisplayName("Course")]
public string CourseName { get; set; }
public int? AuthorId { get; set; }
public Instructor? Author { get; set; }
public int? EditorId { get; set; }
public Instructor? Editor { get; set; }
}
Fluent API:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasOne(course => course.Editor)
.WithMany(editor => editor.CoursesEdited)
.HasForeignKey(course => course.EditorId);
modelBuilder.Entity<Course>()
.HasOne(course => course.Author)
.WithMany(editor => editor.CoursesAuthored)
.HasForeignKey(course => course.AuthorId);
}
和视图部分已提供。希望这些信息对你有所帮助。
英文:
I am trying to update columns in the database for a Courses
table, but get the following error:
> The entity type 'List<Course>' was not found. Ensure that the entity type has been added to the model.
Basically I'm trying to get the state of the check boxes. For example if a course is selected for an Instructor as an author of that course, then enter the instructor's Id into the Course.AuthorId
Here is the method in the controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditInstructorCourse(int id, List<EditInstructorCourseViewModel> model)
{
if (_context.Courses is null)
{
return NotFound();
}
var courses = await _context.Courses.ToListAsync();
try
{
for (int i = 0; i < model.Count; i++)
{
if (model[i].IsAuthor = true)
{
courses[i].AuthorId = id;
_context.Update(courses);
}
else
{
courses[i].AuthorId = null;
}
}
_context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
throw;
}
return RedirectToAction(nameof(Index));
}
Here is the view model
public class EditInstructorCourseViewModel
{
public string CourseId { get; set; }
public string CourseName { get; set; }
public string CourseAuthor { get; set; }
public string CourseEditor { get; set; }
public bool IsAuthor { get; set; }
public bool IsEditor { get; set; }
}
The model classes:
public class Instructor
{
public int Id { get; set; }
[DisplayName("Instructor")]
public string InstructorName { get; set; }
[ForeignKey("AuthorId")]
[DisplayName("Author Of")]
public ICollection<Course> CoursesAuthored { get; set; } = new List<Course>();
[ForeignKey("EditorId")]
[DisplayName("Editor Of")]
public List<Course> CoursesEdited { get; set; } = new List<Course>();
}
public class Course
{
public int Id { get; set; }
[DisplayName("Course")]
public string CourseName { get; set; }
public int? AuthorId { get; set; }
public Instructor? Author { get; set; }
public int? EditorId { get; set; }
public Instructor? Editor { get; set; }
}
Fluent API
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasOne(course => course.Editor)
.WithMany(editor => editor.CoursesEdited)
.HasForeignKey(course => course.EditorId);
modelBuilder.Entity<Course>()
.HasOne(course => course.Author)
.WithMany(editor => editor.CoursesAuthored)
.HasForeignKey(course => course.AuthorId);
}
And the view
@model List<OneToManyMany.Models.ViewModels.EditInstructorCourseViewModel>
@{
var instructorId = ViewData["InstructorId"];
var instructorName = ViewData["InstructorName"];
}
<h2>Edit Courses for <strong> @instructorName</strong></h2>
<div class="form-group">
<label asp-for="@instructorName" class="control-label"></label>
<input asp-for="@instructorName" class="form-control" />
<span asp-validation-for="@instructorName" class="text-danger"></span>
</div>
<div>
<form asp-action="EditInstructorCourse" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="@instructorId" />
<div class="form-group">
<h5>Author Of</h5>
@for (int i = 0; i < Model.Count; i++)
{
<div class="form-check m-1">
<input type="hidden" asp-for="@Model[i].CourseId" />
<input type="hidden" asp-for="@Model[i].CourseName" />
<input asp-for="@Model[i].IsAuthor" class="form-check-input" />
<label class="form-check-label" asp-for="@Model[i].IsAuthor">
@Model[i].CourseName
</label>
</div>
}
</div>
<div class="form-group">
<h5>Editor Of</h5>
@for (int i = 0; i < Model.Count; i++)
{
<div class="form-check m-1">
<input type="hidden" asp-for="@Model[i].CourseId" />
<input type="hidden" asp-for="@Model[i].CourseName" />
<input asp-for="@Model[i].IsEditor" class="form-check-input" />
<label class="form-check-label" asp-for="@Model[i].IsEditor">
@Model[i].CourseName
</label>
</div>
}
</div>
<div class="mt-4">
<a asp-controller="Instructors" asp-action="Index" asp-route-id="@instructorId" class="btn btn-secondary me-2">Cancel</a>
<button id="saveBtn" type="submit" class="btn btn-primary">Save</button>
</div>
</form>
<hr />
</div>
Any help would be greatly appreciated.
答案1
得分: 1
要更新实体:您需要获取实体,更新值,然后调用SaveChangesAsync。在您的情况下,不需要调用update。只需按@ivan建议的方式将其删除。
英文:
To update the entity: you need to get the entity, update the value and call SaveChangesAsync. No need to call update in your case. Just remove it as suggested by @ivan
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论