nickp90
nickp90

Reputation: 81

EntityType: EntitySet 'Orderlines' is based on type 'Orderline' that has no keys defined

I'm trying to access the 'Pizza' model from a controller but am getting the following error - (this works fine before trying to add the OrderLine model):

EntityType 'Orderline' has no key defined. Define the key for this EntityType. Orderlines: EntityType: EntitySet 'Orderlines' is based on type 'Orderline' that has no keys defined.

I keep seeing the issue is adding [Key] but I have done this.

Pizza:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.Domain.Entities
{
    public class Pizza
    {
    [Key]
    public int PizzaId { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Size { get; set; }
    public string Status { get; set; }
    }
}

Orderline

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PizzaShop1.Domain.Entities
{
public class Orderline
{
    [Key]
    public int OrderlineId;

    [ForeignKey("PizzaId")]
    public Pizza PizzaId;
    public decimal OrderlinePrice;
}
}

DbContext

namespace Pizza.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<Pizza> Pizzas { get; set; }
        public DbSet<Orderline> Orderlines { get; set; }
    }
}

Upvotes: 0

Views: 1000

Answers (1)

Maxim Balaganskiy
Maxim Balaganskiy

Reputation: 1574

It has to be a property if I'm remembering correctly.

public class Orderline
{
    [Key]
    public int OrderlineId {get; set;}

    [ForeignKey("PizzaId")]
    public Pizza PizzaId {get; set;}
    public decimal OrderlinePrice {get; set;}
}

Upvotes: 2

Related Questions