Entitytype Has No Key Defined Error

EntityType has no key defined error

The Model class should be changed to :

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
[Table("studentdetails")]
public class student
{
[Key]
public int RollNo { get; set; }

public string Name { get; set; }

public string Stream { get; set; }

public string Div { get; set; }
}
}

EntityType has no key defined error despite defining key

Here is the logic of Scaffolder to check for key field:

First finds all properties with this criteria:

  • Property should be primitive type or if not primitive, one of these: string, decimal, Guid, DateTime, DateTimeOffset, TimeSpan.

Then Tries to find key based on name:

  • [Id] Property (case insensitive)
  • [ClassName][Id] Property (case insensitive)

Tries to find key based on attributes:

  • Property having Key Attribute
  • Property having EdmScalarProperty Attribute with value of EntityKeyPropety=true
  • Property having Column Attribute with value of IsPrimaryKey=true

So you should have a filed in your class that meets one of above criterias.

Also you should remove [Key] from your order property because complex types can not be key.

As an option you can add this property to your class:

[Key]
public int OrderID { get; set; }

And as another option your OrderVM class can be like this:

public partial class OrderVM
{
public OrderVM()
{
orderDetails = new List<OrderDetails>();
}

[Key]
public int OrderID { get; set; }

public DateTime OrderDate { get; set; }

public decimal OrderAmount { get; set; }

[Required]
[StringLength(100)]
public string CustomerName { get; set; }

[StringLength(200)]
public string CustomerAddress { get; set; }

public List<OrderDetail> orderDetails { get; set; }
}

Entity type has no key defined EF6

If you are using EF Code First (not specified in your question), you will need to change the ContactId property name to ContactsId to match the convention of ClassName + Id in order to define the key for your Contacts entity type.

See the MSDN Code First Conventions: http://msdn.microsoft.com/en-us/data/jj679962.aspx

EntityType has no key defined-- Composite Key, Database First

The error "EntityType '{x}' has no key defined. Define the key for this EntityType." indicates that the DBSet is pointing at a class other than your above Entity definition. In the DbContext, using the in DbSet<CurriculumPersonType> right click on CurriculumPersonType and "Go to Definition". My hunch is that you have another class called CurriculumPersonType defined that is being referenced by the DbContext (such as an auto-generated class). If the classes are in different assemblies, make sure all assemblies are built & up to date, though referenced assemblies should automatically be rebuilt when changed.

When you have an entity with a composite key using just [Key] without the column order the exception message is:

"Unable to determine composite primary key ordering for type '{x}'. Use the ColumnAttribute (see http://go.microsoft.com/fwlink/?LinkId=386388) or the HasKey method (see http://go.microsoft.com/fwlink/?LinkId=386387) to specify an order for composite primary keys."

Column order can be assigned by using [Key, Column(Order=0)] No need to specify the column name in the attribute if the property name matches. Using orders of 0,1 or 1,2 are fine, it's just used to order the columns in the key.

What can cause an Entity Type has no key defined error besides having no key defined?

The Entity Framework doesn't support properties (key or otherwise) of type uint. Try this experiment:

Change your Todo.Id property to type int (or long). It will work fine.

Add a non-key property of type uint. It will still scaffold your migration successfully, but it will completely ignore the uint property, just as if you had excluded it explicitly.

There's a suggestion to add support for uint at Support unisgned integer, and the EF team says it will be supported in EF7.

EntityType has no key defined, despite using [Key]

Everything must be given to EntityFramework when use reflection to detect PropertyInfo instances, only use those that are public.You just need to set BookId property as public:

public class Book
{
[Display(Name = "Id:")]
[Key]
public int BookId { get; set; }

[Display(Name = "Title:")]
[MaxLength(35)]
[Required]
private string Title { get; set; }

[Display(Name = "Description:")]
[MaxLength(300)]
private string Description { get; set; }
}

EF 6 - EntityType has no key defined when running Enable-Migrations

As stated by @HenkHolterman, This is most likely being caused by having a custom class with the same name as a class from the .Net Framework. Entity Framework cannot be guaranteed to select your Task class rather than System.Threading.Tasks.Task unless you Specifically refer to it by the full namespaced reference.



Related Topics



Leave a reply



Submit