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

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

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&lt;IActionResult&gt; EditInstructorCourse(int id, List&lt;EditInstructorCourseViewModel&gt; model)
{
        if (_context.Courses is null)
        {
            return NotFound();
        }

        var courses = await _context.Courses.ToListAsync();

        try
        {
            for (int i = 0; i &lt; 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(&quot;Instructor&quot;)]
    public string InstructorName { get; set; }

    [ForeignKey(&quot;AuthorId&quot;)]
    [DisplayName(&quot;Author Of&quot;)]
    public ICollection&lt;Course&gt; CoursesAuthored { get; set; } = new List&lt;Course&gt;();

    [ForeignKey(&quot;EditorId&quot;)]
    [DisplayName(&quot;Editor Of&quot;)]
    public List&lt;Course&gt; CoursesEdited { get; set; } = new List&lt;Course&gt;();
}

public class Course
{
    public int Id { get; set; }

    [DisplayName(&quot;Course&quot;)]
    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&lt;Course&gt;()
        .HasOne(course =&gt; course.Editor)
        .WithMany(editor =&gt; editor.CoursesEdited)
        .HasForeignKey(course =&gt; course.EditorId);

    modelBuilder.Entity&lt;Course&gt;()
        .HasOne(course =&gt; course.Author)
        .WithMany(editor =&gt; editor.CoursesAuthored)
        .HasForeignKey(course =&gt; course.AuthorId);
}

And the view

@model List&lt;OneToManyMany.Models.ViewModels.EditInstructorCourseViewModel&gt;

@{
    var instructorId = ViewData[&quot;InstructorId&quot;];
    var instructorName = ViewData[&quot;InstructorName&quot;];
}

&lt;h2&gt;Edit Courses for &lt;strong&gt; @instructorName&lt;/strong&gt;&lt;/h2&gt;


&lt;div class=&quot;form-group&quot;&gt;
    &lt;label asp-for=&quot;@instructorName&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
    &lt;input asp-for=&quot;@instructorName&quot; class=&quot;form-control&quot; /&gt;
    &lt;span asp-validation-for=&quot;@instructorName&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;

&lt;div&gt;
    &lt;form asp-action=&quot;EditInstructorCourse&quot; method=&quot;post&quot;&gt;
        &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
        &lt;input type=&quot;hidden&quot; asp-for=&quot;@instructorId&quot; /&gt;
        &lt;div class=&quot;form-group&quot;&gt;
            &lt;h5&gt;Author Of&lt;/h5&gt;
            @for (int i = 0; i &lt; Model.Count; i++)
            {
                &lt;div class=&quot;form-check m-1&quot;&gt;
                    &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseId&quot; /&gt;
                    &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseName&quot; /&gt;
                    &lt;input asp-for=&quot;@Model[i].IsAuthor&quot; class=&quot;form-check-input&quot; /&gt;
                    &lt;label class=&quot;form-check-label&quot; asp-for=&quot;@Model[i].IsAuthor&quot;&gt;
                        @Model[i].CourseName
                    &lt;/label&gt;
                &lt;/div&gt;
            }
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
            &lt;h5&gt;Editor Of&lt;/h5&gt;
            @for (int i = 0; i &lt; Model.Count; i++)
            {
                &lt;div class=&quot;form-check m-1&quot;&gt;
                    &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseId&quot; /&gt;
                    &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model[i].CourseName&quot; /&gt;
                    &lt;input asp-for=&quot;@Model[i].IsEditor&quot; class=&quot;form-check-input&quot; /&gt;
                    &lt;label class=&quot;form-check-label&quot; asp-for=&quot;@Model[i].IsEditor&quot;&gt;
                        @Model[i].CourseName
                    &lt;/label&gt;
                &lt;/div&gt;
            }
        &lt;/div&gt;
        &lt;div class=&quot;mt-4&quot;&gt;
            &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;
            &lt;button id=&quot;saveBtn&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Save&lt;/button&gt;
        &lt;/div&gt;
    &lt;/form&gt;
    &lt;hr /&gt;
&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
匿名

发表评论

匿名网友

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

确定