{“无效的列名'SessionStatus_Id'。”}。列`Id`以表名为前缀?

时间:2018-06-01 19:24:10

标签: c# entity-framework ado.net entity-framework-6

我有以下模型类。该程序使用EF V6.2.0

public partial class SessionStatus
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public SessionStatus()
    {
        Sessions = new HashSet<Session>();
    }

    [Key]
    [Column("Id", TypeName ="TinyInt")] // No effect
    public byte Id { get; set; } // Becomes SessionStatus_Id for DB operations

    [Required]
    [StringLength(10)]
    public string Status { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Session> Sessions { get; set; }
}

Session类是

public partial class Session
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Session()
    {
        SessionMessages = new HashSet<SessionMessage>();
    }

    [Key]
    public Guid Uid { get; set; }

    // ......

    public virtual SessionStatus SessionStatus { get; set; }
}

以下代码

        using (var ctx = new ProcessContext())
        {
            ctx.Sessions.Add(new Session { Uid = guid, //...... });
            ctx.SaveChanges();
        }

得到错误

  

{“无效的列名'SessionStatus_Id'。”}

数据库表SessionStatus的列名为Id,而不是SessionStatus_Id(表列SessionStatus_Id不存在)。为什么EF在列名前面加上表名?即使属性[Column("Id", TypeName ="TinyInt")]也无济于事?

1 个答案:

答案 0 :(得分:0)

尝试暂时更改名称而不是Id,您将得到答案......