简体   繁体   English

EntityType''没有定义键。 定义此EntityType的键。 -C#Web API实体框架

[英]EntityType ' ' has no key defined. Define the key for this EntityType. - C# Web API Entity Framework

I'm getting the following error, even though I added the [key] attribute after reading about it on this forum: 即使在此论坛上阅读了[key]属性,我仍然遇到以下错误:

EntityType 'EbayItem' has no key defined. EntityType'EbayItem'没有定义键。 Define the key for this EntityType 定义此EntityType的键

Here is my context class: 这是我的context类:

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

Here is my EbayItem.cs 这是我的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; }
}

Here is my EbayUser.cs 这是我的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; }
}

Here is my Get() 这是我的Get()

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

    return items;
}

Here are screen grabs of my table designs: 这是我的桌子设计的屏幕截图:

tnl_users

tbl_items

Excuse the poor naming conventions as I'm just using this particular project as a playground to venture a but more deeper with Entity Framework. 不好意思的命名约定是因为我只是将这个特定项目用作游乐场来冒险使用Entity Framework,但要更深入一些。

Do you have 'Key' attribute with a capital letter? 您是否具有带有大写字母的“关键字”属性? Or you can also specify column id name, like: How to Specify Primary Key Name in EF-Code-First 或者,您也可以指定列ID名称,例如: 如何在EF-Code-First中指定主键名称

This is probably because you haven't defined a key for 'EbayItem' table. 这可能是因为您尚未为“ EbayItem”表定义键。 You can find more info here: 您可以在此处找到更多信息:

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

Remeber that you need to use the conventions for mapping tables properly if you don't want to use mapping attributes. 请记住,如果您不想使用映射属性,则需要正确使用约定来映射表。 Just add [key] on your EbayItem table property: 只需在您的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; }
}

If you have problems mapping the relationships I recommend you to check the naming conventions OR is Fluent API for explicitly naming columns and relationships https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships 如果您在映射关系时遇到问题,建议您检查命名约定,或者使用Fluent API明确命名列和关系https://docs.microsoft.com/zh-cn/ef/ef6/modeling/code-first/fluent /关系

You should specify that EbayItem.User_ID is a foreign key for the EbayItem.User navigation property. 您应该指定EbayItem.User_IDEbayItem.User导航属性的外键。 You can do this with the ForeignKey attribute. 您可以使用ForeignKey属性执行此操作。 Also use the virtual keyword on the navigation property. 还要在导航属性上使用虚拟关键字。

[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; }
}

暂无
暂无

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

相关问题 实体框架4.3.1 - &gt; 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&#39;{ClassMap}&#39;”未定义键。 定义此EntityType的密钥。” - EF 5.0 Fluent mapping: “EntityType '{ClassMap}' has no key defined. Define the key for this EntityType.” 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“帖子”没有定义键。 定义此EntityType的键 - EntityType 'posts' has no key defined. Define the key for this EntityType EntityType&#39;MyProfile&#39;没有定义键。 定义此EntityType的键 - EntityType 'MyProfile' has no key defined. Define the key for this EntityType EntityType &#39;Worker&#39; 没有定义键。 定义此 EntityType 的键 - EntityType 'Worker' has no key defined. Define the key for this EntityType 实体框架验证错误没有定义键。 定义此EntityType的键 - Entity framework Validation errors 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&#39;表&#39;没有定义键。 定义此EntityType的键 - Database First EntityType 'table' has no key defined. Define the key for this EntityType
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM