System.Data.SqlClient.SqlException:列名“Root_rootID”无效

System.Data.SqlClient.SqlException: Invalid column name 'Root_rootID'

提问人:siddhesh 提问时间:2/25/2023 最后编辑:marc_ssiddhesh 更新时间:2/25/2023 访问量:34

问:

我是 ASP.NET 的新手,我正在学习 MVC 和 EF。使用“数据库中的代码优先”。ASP.NET 表中自动创建的。甚至 SQL 数据库中的表也没有列。rootIdmarriageMarriagerootId

这是表类marriage

[Table("Marriage")]
public partial class Marriage
{
    [Key]
    public int marriageID { get; set; }

    public int? spouseID { get; set; } = 0;
    public int? childID { get; set; } = 0;

    public virtual ICollection<Child> children { get; set; }

    public virtual Spouse spouse { get; set; }
}

这是我的根表类:

[Table("Root")]
public partial class Root
{
    [Key]
    public int rootID { get; set; }

    [Required]
    [StringLength(100)]
    public string name { get; set; }

    [Column("class")]
    [Required]
    [StringLength(100)]
    public string @class { get; set; }

    [Required]
    [StringLength(100)]
    public string textClass { get; set; } = "emphasis";

    public int? extraID { get; set; }
    public int? marriagesID { get; set; }

    public DateTime whenEntered { get; set; } = DateTime.UtcNow;
    public DateTime whenModified { get; set; } = DateTime.UtcNow;

    public virtual ICollection<Marriage> marriages { get; set; }

    public virtual Extra  extra  { get; set; }
}

每当我像在模型中一样调用婚姻类或将数据保存在婚姻模型中时,只需将性别化即可

Sql = "SELECT \r\n    [Extent1].[marriageID] AS [marriageID],
  [Extent1].[spouseID] AS [spouseID],
[Extent1].[childID] AS [childID], 
  [Extent1].[Root_rootID] AS [Root_rootID]
  FROM [dbo].[Marriage] AS [Extent1]"

因为不在类中,也不在数据库表中。为什么会发生此异常?我错过了什么?[Root_rootID]Marriage

SQL asp.net ASP.NET-MVC 实体框架 异常

评论

1赞 nbk 2/25/2023
婚姻没有这样的列,你需要通过mariagId加入Root,并从那里获取值

答: 暂无答案