Search code examples
c#entity-frameworkcode-first

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


I am using Visual Studio 2015 and entity framework version 6.0.0.0 (code first), I've created a class as:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace IRANMVCCore.Domain.Entity
{
   public class UserProperty
    {
        [key]
        [ForeignKey("Instructor")]
        [Required(ErrorMessage = "error")]
        public string Name { get; set; }

        [Required(ErrorMessage = "error")]
        public string Famil { get; set; }

        [Required(ErrorMessage = "error")]
        public string City { get; set; }

        [Required(ErrorMessage = "error")]
        public string Countries { get; set; }
    }
}

Now I am getting this error:

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

I have reference : Refrances

How can I solved it?


Solution

  • 1) To solve it check your reference version of:

    System.ComponentModel.DataAnnotations

    2) [key] should be [Key]

    Hope this helps anyone else