简体   繁体   English

EntityType“帖子”没有定义键。 定义此EntityType的键

[英]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. 使用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. 旋转一下,看看是否适合您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityType没有定义键。 定义此EntityType的键 - EntityType has no key defined. Define the key for this EntityType EntityType X没有定义键。 定义此EntityType的键 - EntityType X has no key defined. Define the key for this EntityType EntityType'MyProfile'没有定义键。 定义此EntityType的键 - EntityType 'MyProfile' has no key defined. Define the key for this EntityType EntityType 'Worker' 没有定义键。 定义此 EntityType 的键 - EntityType 'Worker' has no key defined. Define the key for this EntityType EntityType''没有定义键。 定义此EntityType的键。 -C#Web API实体框架 - EntityType ' ' has no key defined. Define the key for this EntityType. - C# Web API Entity Framework 实体框架4.3.1 - > 5.0异常:“EntityType没有定义键。 定义此EntityType的键。“ - Entity Framework 4.3.1 -> 5.0 Exception: “EntityType has no key defined. Define the key for this EntityType.” EF 5.0 Fluent映射:“ EntityType'{ClassMap}'”未定义键。 定义此EntityType的密钥。” - EF 5.0 Fluent mapping: “EntityType '{ClassMap}' has no key defined. Define the key for this EntityType.” 模型验证错误:EntityType 未定义键。 定义此 EntityType 的键 - Model validation error: EntityType has no key defined. Define the key for this EntityType 数据库第一个EntityType'表'没有定义键。 定义此EntityType的键 - Database First EntityType 'table' has no key defined. Define the key for this EntityType 代码优先-EntityType'编译器结果'尚未定义键。 定义此EntityType的键 - Code First - EntityType 'Compiler Results' has no key defined. Define the key for this EntityType
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM