问题描述:

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; }

}

}

网友答案:

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; }
  }
}
网友答案:

When you use an ORM (object-relational mapper) framework like NHibernate or Entity framework that helps you map a relational database to an object model you need something that will let you make a meaningful relation between your objects in memory and rows of data in your database and that thing is a key (id as NHibernate call it) and usually that's the natural way that RDBMS track records using a Primary key (usually you use DB primary key as the key of your object) When you check to see if two objects are equal using == operator you are checking that those objects have the same reference (or address in memory). This kind of equality is not very helpful when you are using an ORM .You can load multiple instances of a record in memory with different references so that it's impossible to check the equality of objects by their references .That's when checking equality by value come into play and keys have the main role in this play.

相关阅读:
Top