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

c# entity-framework

Question

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?

1
5
9/25/2014 6:06:56 PM

Accepted Answer

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.

11
9/25/2014 6:14:30 PM

Popular Answer

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;


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow