1

我的模型课是

public class posts
{
    [Key]
    public int AuthorId;

    public string author_first_name;
    public string author_last_name;
    public string post_text;
}

我不明白为什么这么说。即使我遵循命名约定并且仍然插入了关键注释以进行安全测量,它仍然给我错误。

4

1 回答 1

6

使用 EntityFramework 声明实体时,请使用属性,而不是字段。

你应该有:

public class Post
{
    [Key]
    public int AuthorId { get; set; }

    public string AuthorFirstName { get; set; }
    public string AuthorLastName { get; set; }
    public string PostText { get; set; }
}

当您使用这些数据注释声明属性时,它们必须出现在公共 getter 上。

试一试,看看它是否适合你。

于 2013-03-16T10:47:44.583 回答