Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 EntityType has no key defined

I want to display customer information. Then I created some classes; Customer, Delivery, Order, OrderLine, Product, and rentalDB. rentalDB class sets 5 DbSet of Product, Customer, Order, OrderLine, and Delivery. When I make UserController with list view, I cannot display the customer information, and it takes errors:

One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'OrderLine' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'Delivery' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �OrderLine� is based on type �OrderLine� that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Delivery� is based on type �Delivery� that has no keys defined.

I don't know why these entities require key? I have no idea for this error.. Could you help me?

--UserController.cs--

namespace MvcApplication2.Controllers
{
public class UserController : Controller
  {
    //
    // GET: /User/
    rentalDB _db = new rentalDB();

    public ActionResult Index()
    {
        var model = _db.Customer;
        return View(model);
    }
  }
}

--Delivery.cs in Models folder--

namespace MvcApplication2.Models
{
  public class Delivery
  {
    public int trackId { get; set; }
    public String address { get; set; }
    public String postCode { get; set; }
    public decimal deliveryPrice { get; set; }
    public DateTime deliveryDate { get; set; }
    public DateTime returnDate { get; set; }
  }
}

--OrderLine.cs in Models folder--

namespace MvcApplication2.Models
{
   public class OrderLine
   {
    public int basketId { get; set; }
    public int productId { get; set; }
    public int quantity { get; set; }
   }
}
like image 926
wholee1 Avatar asked Feb 18 '12 19:02

wholee1


1 Answers

In order to use the entity framework, every entity needs a key. This is how EF tracks objects in its cache, posts updates back to the underlying data store, and links related objects together.

Yours objects already have keys, you just need to tell the EF about them:

namespace MvcApplication2.Models
{
  public class Delivery
  {
    [Key] public int trackId { get; set; }
    public String address { get; set; }
    public String postCode { get; set; }
    public decimal deliveryPrice { get; set; }
    public DateTime deliveryDate { get; set; }
    public DateTime returnDate { get; set; }
  }
}
like image 66
Michael Edenfield Avatar answered Sep 29 '22 15:09

Michael Edenfield