繁体   English   中英

Contoso 大学项目:InvalidCastException:无法将“System.String”类型的对象转换为“System.Int32”类型

[英]Contoso University Project: InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'

我正在 .net core 中处理 Contoso 大学项目,当我从导航菜单中选择“学生”以查看学生列表,然后在每个学生姓名下的页面上选择“详细信息”链接时,我收到以下错误:

InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'.

它说错误在这一行的 StudentsController.cs 中:

+
37            var student = await _context.Students

关于问题可能是什么的任何建议都会非常有帮助。

这是我的控制器代码:

// 学生控制器.cs

// GET: Students/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var student = await _context.Students
                .Include(s => s.Enrollments)
                    .ThenInclude(e => e.Course)
                .AsNoTracking()
                .FirstOrDefaultAsync(m => m.ID == id);
            if (student == null)
            {
                return NotFound();
            }

            return View(student);
        }

// SchoolContext.cs

    namespace ContosoUniversity.Data
    {
        public class SchoolContext : DbContext
        {
            public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
            {
            }
    
            public DbSet<Course> Courses { get; set; }
            public DbSet<Enrollment> Enrollments { get; set; }
            public DbSet<Student> Students { get; set; }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Course>().ToTable("Course");
                modelBuilder.Entity<Enrollment>().ToTable("Enrollment");
                modelBuilder.Entity<Student>().ToTable("Student");
            }
        }
    }

// Student.cs(模型)

    namespace ContosoUniversity.Models
    {
        public class Student
        {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }
            public DateTime EnrollmentDate { get; set; }
            public ICollection<Enrollment> Enrollments { get; set; }
        }
    }

// Index.cshtml(列表视图)

    @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstMidName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.EnrollmentDate)
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.ID">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
                </td>
            </tr>
    }

// 注册.cs

    namespace ContosoUniversity.Models
    {
   }
        public class Enrollment
        {
            public int EnrollmentID { get; set; }
    
            public int CourseID { get; set; }
    
            public int StudentID { get; set; } // foreign key
    
    // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
            public string Grade { get; set; } // nullable
    // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
    
            public Course Course { get; set; } // foreign key
    
            public Student Student { get; set; }
    
        }
    }

我的数据结构:

dbo.Course:
CourseId (PK, int, not null)
Title (varchar(255), not null)
Credits (int, null)

dbo.Enrollment
EnrollmentId (PK, int, not null)
StudentID( int, null)
CourseID (int, null)
Grade (varchar(255), null)

Student
Id(PK, int, not null)
FirstMidName(varchar(255), null)
LastName(varchar(255), null)
EnrollmentDate(datetime, null)

看看这一行:

var student = await _context.Students
         .Include(s => s.Enrollments)
         .ThenInclude(e => e.Course)
         .AsNoTracking()
         .FirstOrDefaultAsync(m => m.ID == id);

你传入了一个可为空的 int?

public async Task<IActionResult> Details(int? id)

在课堂上它只是 int

   public int ID { get; set; }

尝试将可空值 (int?) 转换回 (int) 之类的

 var student = await _context.Students
            .Include(s => s.Enrollments)
            .ThenInclude(e => e.Course)
            .AsNoTracking()
            .FirstOrDefaultAsync(m => m.ID == (int)id);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM