简体   繁体   中英

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:

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

Here is my context class:

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

Here is my 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

[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: 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.

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

This is probably because you haven't defined a key for 'EbayItem' table. You can find more info here:

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:

[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

You should specify that EbayItem.User_ID is a foreign key for the EbayItem.User navigation property. You can do this with the ForeignKey attribute. 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; }
}

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