簡體   English   中英

EntityType“帖子”沒有定義鍵。 定義此EntityType的鍵

[英]EntityType 'posts' has no key defined. Define the key for this EntityType

我的模特班是

public class posts
{
    [Key]
    public int AuthorId;

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

我不明白為什么這么說。 即使我遵循命名約定並且仍然插入了用於安全措施的鍵注釋,它仍然給我錯誤。

使用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; }
}

使用這些數據注釋聲明屬性時,它們必須出現在公共獲取器上。

旋轉一下,看看是否適合您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM