Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'Key' could not be found

The [Key] attribute for entity framework is not working for me. I have a console application and I am trying to add this class:

public class Post
{
    [Key]
    public int IDPost { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int IDBlog { get; set; }
    public virtual Blog Blog { get; set; }
}

I get these errors:

The type or namespace name 'Key' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'KeyAttribute' could not be found (are you missing a using directive or an assembly reference?)

I have added this using:

using System.Data.Entity;

And I have these references added to the project:

EntityFramework
EntityFramework.SqlServer
System.ComponentModel.DataAnnotations

I am using EF6 and VS2012.

What reference am I missing?

like image 381
Guerrilla Avatar asked Sep 25 '14 18:09

Guerrilla


2 Answers

In EF6, System.Data.Entity has been replaced with System.Data.Entity.Core. Make sure that you are no longer referencing any EF5 dlls and replace your using inclusion with

System.Data.Entity.Core

In addition, [Key] comes from the

System.ComponentModel.DataAnnotations

namespace. If you have it included in the using statements for your class, it should compile.

like image 148
David L Avatar answered Sep 19 '22 09:09

David L


For myself I had a lowercase [key] instead of [Key], so be sure to check for this when you come across this issue.

like image 20
Steven Halla Avatar answered Sep 21 '22 09:09

Steven Halla