EF Core: 实体类型 ‘List‘ 未找到。确保已将实体类型添加到模型中

huangapple go评论95阅读模式
英文:

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中。

这是控制器中的方法:

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public async Task<IActionResult> EditInstructorCourse(int id, List<EditInstructorCourseViewModel> model)
  4. {
  5. if (_context.Courses is null)
  6. {
  7. return NotFound();
  8. }
  9. var courses = await _context.Courses.ToListAsync();
  10. try
  11. {
  12. for (int i = 0; i < model.Count; i++)
  13. {
  14. if (model[i].IsAuthor = true)
  15. {
  16. courses[i].AuthorId = id;
  17. _context.Update(courses);
  18. }
  19. else
  20. {
  21. courses[i].AuthorId = null;
  22. }
  23. }
  24. _context.SaveChangesAsync();
  25. }
  26. catch (DbUpdateConcurrencyException)
  27. {
  28. throw;
  29. }
  30. return RedirectToAction(nameof(Index));
  31. }

这是视图模型:

  1. public class EditInstructorCourseViewModel
  2. {
  3. public string CourseId { get; set; }
  4. public string CourseName { get; set; }
  5. public string CourseAuthor { get; set; }
  6. public string CourseEditor { get; set; }
  7. public bool IsAuthor { get; set; }
  8. public bool IsEditor { get; set; }
  9. }

模型类:

  1. public class Instructor
  2. {
  3. public int Id { get; set; }
  4. [DisplayName("Instructor")]
  5. public string InstructorName { get; set; }
  6. [ForeignKey("AuthorId")]
  7. [DisplayName("Author Of")]
  8. public ICollection<Course> CoursesAuthored { get; set; } = new List<Course>();
  9. [ForeignKey("EditorId")]
  10. [DisplayName("Editor Of")]
  11. public List<Course> CoursesEdited { get; set; } = new List<Course>();
  12. }
  13. public class Course
  14. {
  15. public int Id { get; set; }
  16. [DisplayName("Course")]
  17. public string CourseName { get; set; }
  18. public int? AuthorId { get; set; }
  19. public Instructor? Author { get; set; }
  20. public int? EditorId { get; set; }
  21. public Instructor? Editor { get; set; }
  22. }

Fluent API:

  1. protected override void OnModelCreating(ModelBuilder modelBuilder)
  2. {
  3. modelBuilder.Entity<Course>()
  4. .HasOne(course => course.Editor)
  5. .WithMany(editor => editor.CoursesEdited)
  6. .HasForeignKey(course => course.EditorId);
  7. modelBuilder.Entity<Course>()
  8. .HasOne(course => course.Author)
  9. .WithMany(editor => editor.CoursesAuthored)
  10. .HasForeignKey(course => course.AuthorId);
  11. }

和视图部分已提供。希望这些信息对你有所帮助。

英文:

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

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public async Task&lt;IActionResult&gt; EditInstructorCourse(int id, List&lt;EditInstructorCourseViewModel&gt; model)
  4. {
  5. if (_context.Courses is null)
  6. {
  7. return NotFound();
  8. }
  9. var courses = await _context.Courses.ToListAsync();
  10. try
  11. {
  12. for (int i = 0; i &lt; model.Count; i++)
  13. {
  14. if (model[i].IsAuthor = true)
  15. {
  16. courses[i].AuthorId = id;
  17. _context.Update(courses);
  18. }
  19. else
  20. {
  21. courses[i].AuthorId = null;
  22. }
  23. }
  24. _context.SaveChangesAsync();
  25. }
  26. catch (DbUpdateConcurrencyException)
  27. {
  28. throw;
  29. }
  30. return RedirectToAction(nameof(Index));
  31. }

Here is the view model

  1. public class EditInstructorCourseViewModel
  2. {
  3. public string CourseId { get; set; }
  4. public string CourseName { get; set; }
  5. public string CourseAuthor { get; set; }
  6. public string CourseEditor { get; set; }
  7. public bool IsAuthor { get; set; }
  8. public bool IsEditor { get; set; }
  9. }

The model classes:

  1. public class Instructor
  2. {
  3. public int Id { get; set; }
  4. [DisplayName(&quot;Instructor&quot;)]
  5. public string InstructorName { get; set; }
  6. [ForeignKey(&quot;AuthorId&quot;)]
  7. [DisplayName(&quot;Author Of&quot;)]
  8. public ICollection&lt;Course&gt; CoursesAuthored { get; set; } = new List&lt;Course&gt;();
  9. [ForeignKey(&quot;EditorId&quot;)]
  10. [DisplayName(&quot;Editor Of&quot;)]
  11. public List&lt;Course&gt; CoursesEdited { get; set; } = new List&lt;Course&gt;();
  12. }
  13. public class Course
  14. {
  15. public int Id { get; set; }
  16. [DisplayName(&quot;Course&quot;)]
  17. public string CourseName { get; set; }
  18. public int? AuthorId { get; set; }
  19. public Instructor? Author { get; set; }
  20. public int? EditorId { get; set; }
  21. public Instructor? Editor { get; set; }
  22. }

Fluent API

  1. protected override void OnModelCreating(ModelBuilder modelBuilder)
  2. {
  3. modelBuilder.Entity&lt;Course&gt;()
  4. .HasOne(course =&gt; course.Editor)
  5. .WithMany(editor =&gt; editor.CoursesEdited)
  6. .HasForeignKey(course =&gt; course.EditorId);
  7. modelBuilder.Entity&lt;Course&gt;()
  8. .HasOne(course =&gt; course.Author)
  9. .WithMany(editor =&gt; editor.CoursesAuthored)
  10. .HasForeignKey(course =&gt; course.AuthorId);
  11. }

And the view

  1. @model List&lt;OneToManyMany.Models.ViewModels.EditInstructorCourseViewModel&gt;
  2. @{
  3. var instructorId = ViewData[&quot;InstructorId&quot;];
  4. var instructorName = ViewData[&quot;InstructorName&quot;];
  5. }
  6. &lt;h2&gt;Edit Courses for &lt;strong&gt; @instructorName&lt;/strong&gt;&lt;/h2&gt;
  7. &lt;div class=&quot;form-group&quot;&gt;
  8. &lt;label asp-for=&quot;@instructorName&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
  9. &lt;input asp-for=&quot;@instructorName&quot; class=&quot;form-control&quot; /&gt;
  10. &lt;span asp-validation-for=&quot;@instructorName&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
  11. &lt;/div&gt;
  12. &lt;div&gt;
  13. &lt;form asp-action=&quot;EditInstructorCourse&quot; method=&quot;post&quot;&gt;
  14. &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
  15. &lt;input type=&quot;hidden&quot; asp-for=&quot;@instructorId&quot; /&gt;
  16. &lt;div class=&quot;form-group&quot;&gt;
  17. &lt;h5&gt;Author Of&lt;/h5&gt;
  18. @for (int i = 0; i &lt; Model.Count; i++)
  19. {
  20. &lt;div class=&quot;form-check m-1&quot;&gt;
  21. &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseId&quot; /&gt;
  22. &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseName&quot; /&gt;
  23. &lt;input asp-for=&quot;@Model[i].IsAuthor&quot; class=&quot;form-check-input&quot; /&gt;
  24. &lt;label class=&quot;form-check-label&quot; asp-for=&quot;@Model[i].IsAuthor&quot;&gt;
  25. @Model[i].CourseName
  26. &lt;/label&gt;
  27. &lt;/div&gt;
  28. }
  29. &lt;/div&gt;
  30. &lt;div class=&quot;form-group&quot;&gt;
  31. &lt;h5&gt;Editor Of&lt;/h5&gt;
  32. @for (int i = 0; i &lt; Model.Count; i++)
  33. {
  34. &lt;div class=&quot;form-check m-1&quot;&gt;
  35. &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseId&quot; /&gt;
  36. &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseName&quot; /&gt;
  37. &lt;input asp-for=&quot;@Model[i].IsEditor&quot; class=&quot;form-check-input&quot; /&gt;
  38. &lt;label class=&quot;form-check-label&quot; asp-for=&quot;@Model[i].IsEditor&quot;&gt;
  39. @Model[i].CourseName
  40. &lt;/label&gt;
  41. &lt;/div&gt;
  42. }
  43. &lt;/div&gt;
  44. &lt;div class=&quot;mt-4&quot;&gt;
  45. &lt;a asp-controller=&quot;Instructors&quot; asp-action=&quot;Index&quot; asp-route-id=&quot;@instructorId&quot; class=&quot;btn btn-secondary me-2&quot;&gt;Cancel&lt;/a&gt;
  46. &lt;button id=&quot;saveBtn&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Save&lt;/button&gt;
  47. &lt;/div&gt;
  48. &lt;/form&gt;
  49. &lt;hr /&gt;
  50. &lt;/div&gt;

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

huangapple
  • 本文由 发表于 2023年7月17日 09:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701054.html
  • c#
  • entity-framework-core

List: 如何为某个索引元素应用“where”条件? go 89
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码