skip to Main Content

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.

3

Answers


  1. 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://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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; }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search