[英]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: 这是我的桌子设计的屏幕截图:
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_ID
是EbayItem.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.