【问题标题】:EntityType ' ' has no key defined. Define the key for this EntityType. - C# Web API Entity FrameworkEntityType ' ' 没有定义键。定义此 EntityType 的键。 - C# Web API 实体框架
【发布时间】:2019-08-15 20:51:20
【问题描述】:

我收到以下错误,即使我在此论坛上阅读后添加了 [key] 属性:

EntityType 'EbayItem' 没有定义键。定义此 EntityType 的键

这是我的context 课程:

public class PeopleContext : DbContext
{
    public DbSet<EbayItem> EbayItems { get; set; }
    public DbSet<EbayUser> EbayUsers { get; set; }
} 

这是我的EbayItem.cs

[Table("tbl_items")]
public class EbayItem
{
    [key]
    public int Item_ID { get; set; }
    public string Item { get; set; }
    public string Category { get; set; }
    public int Bids { get; set; }

    public int User_ID { get; set; }
    public EbayUser User { get; set; }
}

这是我的EbayUser.cs

[Table("tbl_users")]
public class EbayUser
{
    public int User_ID { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string Age { get; set; }

    public IList<EbayItem> EbayItems { get; set; }
}

这是我的Get()

// GET: api/Ebay
public IEnumerable<EbayItem> Get()
{
    PeopleContext context = new PeopleContext();
    List<EbayItem> items = context.EbayItems.Include(p => p.User).ToList();

    return items;
}

这是我的表格设计的屏幕截图:

请原谅糟糕的命名约定,因为我只是将这个特定项目用作游乐场,以冒险但更深入地使用 Entity Framework。

【问题讨论】:

  • 我在EbayItem 上没有看到[key] 属性,这与错误消息一致。
  • 我已经编辑了我的帖子,[key] 在正确的位置,但它仍然不起作用

标签: c# entity-framework


【解决方案1】:

您有带有大写字母的“Key”属性吗?或者你也可以指定列id名称,比如:How to Specify Primary Key Name in EF-Code-First

【讨论】:

  • 啊!不,我有一个小写字母!请注意,当我转到api/ebay 时,我现在有这个错误type failed to serialize the response body for content type,但我想我必须单独发布这个问题。
  • 是的,我认为你应该关闭这个问题。
【解决方案2】:

这可能是因为您没有为“EbayItem”表定义键。你可以在这里找到更多信息:

https://gilesey.wordpress.com/tag/entitytype-has-no-key-defined/

请记住,如果您不想使用映射属性,则需要正确使用映射表的约定。只需在您的 EbayItem 表属性上添加 [key]:

[Table("tbl_items")]
public class EbayItem
{
    [Key]
    public int Item_ID { get; set; }
    public string Item { get; set; }
    public string Category { get; set; }
    public int Bids { get; set; }

    public int User_ID { get; set; }
    public EbayUser User { get; set; }
}

如果您在映射关系时遇到问题,我建议您检查命名约定或者是用于显式命名列和关系的 Fluent API https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships

【讨论】:

  • 嗨,我已经编辑了我的帖子,因为密钥不在 EbayItem 但现在是。
  • @user1574598 我认为这与糟糕的命名约定有关。我建议您改进名称或使用 Fluent API 显式命名列和关系docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/…
  • 好的,谢谢,我已经删除了Include(p =&gt; p.User).,我得到了xml 数据,但只有user_id 而不是另一个列表。
  • 这是因为您至少需要两件事。如果您希望 EF 正确映射,请将用户属性更改为虚拟并改进名称。或者就像我之前告诉过你的那样:使用 fluent API
【解决方案3】:

您应该指定EbayItem.User_IDEbayItem.User 导航属性的外键。您可以使用ForeignKey 属性执行此操作。还要在导航属性上使用 virtual 关键字。

[Table("tbl_items")]
public class EbayItem
{
    [Key]
    public int Item_ID { get; set; }
    public string Item { get; set; }
    public string Category { get; set; }
    public int Bids { get; set; }

    public int User_ID { get; set; }
    [ForeignKey("User_ID")]
    public virtual EbayUser User { get; set; }
}

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 2016-04-02
    • 2016-03-16
    • 2013-08-04
    • 2012-12-10
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多