【发布时间】:2019-02-13 16:59:48
【问题描述】:
我正在学习使用 asp.net mvc 和实体框架进行 Web 应用程序开发。我有一个包含 3 个学生、性别和项目表的数据库。我遇到了这个问题。
第 23 行出现错误。
列名“Genders_Id”无效。 列名“Programs_Id”无效。
异常详细信息: System.Data.SqlClient.SqlException:列名“Genders_Id”无效。 列名“Programs_Id”无效。
Source Error: Line 21: { Line 22: StudentsContext stdContext = new StudentsContext(); Line 23: Students students = stdContext.Students.Single(std => std.Id == id); Line 24: //ViewBag.Student = students; Line 25: return View(students);
我的 Students.cs 模型代码:
namespace WebApplication4.Models
{
public class Students
{
public string Name { get; set; }
public int Id { get; set; }
public int Gender { get; set; }
public int Program { get; set; }
public string Country { get; set; }
}
}
我的 Context.cs 文件:
public class StudentsContext:DbContext
{
public DbSet<Students> Students { get; set; }
public DbSet<Programs> Programs { get; set; }
public DbSet<Genders> Genders { get; set; }
}
Programs.cs:
public class Programs
{
public int Id { get; set; }
public string Program { get; set; }
public List<Students> Students { get; set; }
}
Genders.cs:
public class Genders
{
public int Id { get; set; }
public string Gender { get; set; }
public List<Students> Students { get; set; }
}
数据库表:
【问题讨论】:
-
你也可以发布
Programs和Genders的课程代码吗?谢谢 -
@D-Shih 用代码编辑了我的帖子。
-
@IvanStoev 已修复。
-
是 dbfirst 还是 codefirst ?
-
@EhsanSajjad db 先创建,程序和性别相关的列和表是后来添加的
标签: c# asp.net asp.net-mvc entity-framework