Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ComponentModel.DataAnnotations.Schema not found

I am running into an issue in Visual Studio 2012 that involves the System.ComponentModel.DataAnnotations.Schema namespace. It tells me that the ForeignKeyAttribute cannot be resolved, the solution in the past was to add the using statement that is commented out below. VS2012 can't resolve the Schema namespace as VS2010 was able to. Has anything changed in recent .Net releases that would be causing this problem? If so, how do I work around them?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
// using System.ComponentModel.DataAnnotations.Schema;

namespace Data
{
    public class AffiliateUser
    {
        [Key, ForeignKey("User")]
        public int UserId { get; set; }

            [StringLength(50)]
            public string AffiliateUserKey { get; set; }

            public Guid ApiKey { get; set; }
            public string PasswordHash { get; set; }
            public bool IsDeleted { get; set; }
        }
}

like image 350
ThirtyApes Avatar asked Oct 19 '12 19:10

ThirtyApes


3 Answers

Are you sure you are targeting .NET 4.5 Framework. ForeignKeyAttribute is only available in .NET 4.5

like image 161
parapura rajkumar Avatar answered Nov 11 '22 03:11

parapura rajkumar


I reinstalled the Entity Framework and it works!

like image 45
Lücks Avatar answered Nov 11 '22 03:11

Lücks


Your code and the System.ComponentModel.DataAnnotations.Schema namespace are correct.

So check your references. This one should be in Assembly System.ComponentModel.DataAnnotations.dll, v4.0.0.0

Did you upgrade the project from Fx 4?

You can fix the version with NuGet.

like image 34
Henk Holterman Avatar answered Nov 11 '22 02:11

Henk Holterman