问题描述
我正在.net核心中的Contoso大学项目中,当我从导航菜单中选择“学生”以查看学生列表时,然后在页面上的“详细信息”链接下以每个人的名字命名学生,我收到以下错误:
InvalidCastException: Unable to cast object of type 'System.String' to type 'system.int32'.
它说此行上的StudentController.cs错误:
+
37 var student = await _context.Students
有关该问题可能是什么的任何建议都会很有帮助。
这是我的控制器代码:
// StudentsController.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>
}
// Enrollment.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,not null)
StudentID( int,null)
CourseID (int,null)
Grade (varchar(255),null)
Student
Id(PK,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);
您传递的是可为null的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);