【问题标题】:System.Data.SqlClient.SqlException: Invalid column name 'Gender_id'System.Data.SqlClient.SqlException:列名“Gender_id”无效
【发布时间】: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; }
}

数据库表:

【问题讨论】:

  • 你也可以发布ProgramsGenders的课程代码吗?谢谢
  • @D-Shih 用代码编辑了我的帖子。
  • @IvanStoev 已修复。
  • 是 dbfirst 还是 codefirst ?
  • @EhsanSajjad db 先创建,程序和性别相关的列和表是后来添加的

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

Genders_IdPrograms_Id 是 FK 列的默认 EF6 常规名称,用于分别由 Genders.StudentsPrograms.Students 集合导航属性引入的一对多关系。

由于您的 FK 属性/列名称不是常规名称,您应该通过 [ForeinKey] 数据注释来指定它们:

public class Programs
{
    // ...
    [ForeignKey("Program")]
    public List<Students> Students { get; set; }
}

public class Genders
{
    // ...
    [ForeignKey("Gender")]
    public List<Students> Students { get; set; }
}

或(我的首选)流畅的 API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // ...

    modelBuilder.Entity<Genders>()
        .HasMany(e => e.Students)
        .WithRequired()
        .HasForeignKey(e => e.Gender);

    modelBuilder.Entity<Programs>()
        .HasMany(e => e.Students)
        .WithRequired()
        .HasForeignKey(e => e.Program);
}

【讨论】:

  • 但是我没有在数据库中使用外键,我正要实现它但想检索学生数据但我遇到了这个问题
  • 不确定发生了什么,但是在指定外键之后(即使我没有在数据库中实现外键)它工作正常。谢谢
  • @DanialAhmed 他的意思是默认情况下,EF 会将类中的外键 ID 视为 Program_Id 或 Student_Id 之类的属性,但您尚未将它们定义为约定,因此您需要明确告知哪一列是外键那些
  • 当您映射到现有数据库(不由 EF 代码迁移维护)时,更重要的是 EF 认为数据库结构是什么(基于实体模型元数据),然后才是实际的数据库结构。虽然最好是让它们保持同步 :) 将列映射为 FK 允许 EF 正确连接相关表。
【解决方案2】:

您的问题是您在数据层的连接字符串和在Web层的连接字符串指向不同的数据库。

例如数据层读取指向测试数据库的开发数据库webapp。

更新连接字符串以指向同一个数据库

确保您的两个数据库具有相同的表和列

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-14
    • 2019-11-02
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多