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?
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.
For myself I had a lowercase [key] instead of [Key], so be sure to check for this when you come across this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With