“字段列表”中的C#未知列“Extent1.UserId”

时间:2016-01-19 16:32:28

标签: c# mysql .net

我正在尝试建模我的数据库,但我遇到了一些问题。我有这两个表:ProductSetting和User。 ProductSetting看起来像这样:

ProductSettingId
ProductType
CreatedByUserId

CreatedByUserId在“user_id”列上设置了一个带有用户表的外键

用户表就像这样

UserId
Name

我的ProductUnitSetting类

public class ProductUnitSetting
{
    [Key, Column("product_setting_id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Column("product_type"), Required]
    public string ProductType { get; set; }

    public int UserId { get; set; }             

    [ForeignKey("UserId")]
    public virtual User User { get; set; }

    [Column("created_by_user_id"), Required]        
    public int CreatedByUserId { get; set; }

}

我的用户类:

[Table("user")]
public class User
{
    [Key, Column("user_id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

}

所以当我运行它时,我收到错误:

Unknown column 'Extent1.UserId' in 'field list'

Exception Details: MySql.Data.MySqlClient.MySqlException: Unknown column     'Extent1.UserId' in 'field list'

我尝试了很多东西,包括将InverseProperty放在用户类中,如:

[InverseProperty("created_by_user_id")]
public virtual ProductUnitSetting ProductUnitSetting { get; set; }

但它不起作用,我不确定我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个

[ForeignKey("User")]
public int UserId { get; set; }             

public virtual User User { get; set; }