Guerrilla
Guerrilla

Reputation: 14866

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?

Upvotes: 9

Views: 23685

Answers (4)

Gopika Murali
Gopika Murali

Reputation: 21

I got the same error, it was because the project wasn't saved.

Upvotes: 0

Steven Halla
Steven Halla

Reputation: 45

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

Upvotes: 1

Tauqeer
Tauqeer

Reputation: 139

I got same error using Visual Studio 2010 when I was creating an asp.net Webforms application. I followed these steps and it worked for me.

  1. open nugetpackage manager and search System.data.entity.
  2. install it.
  3. put reference of using System.ComponentModel.DataAnnotations;

Upvotes: 0

David L
David L

Reputation: 33823

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.

Upvotes: 16

Related Questions