简体   繁体   中英

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

My model class is

public class posts
{
    [Key]
    public int AuthorId;

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

I don't understand why it is saying that. Even though i am following the naming conventions and still have inserted the key annotation for safe measure it is still giving me the error.

Please use Properties, not fields when declaring Entities using EntityFramework.

You should have:

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

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

When you declare attributes using these Data Annotations they must appear on a public getter.

Give this a whirl and see if it works out for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM