Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add attributes to Entity Framework database first generated classes

I am using Entity Framework 4.1 and generating my classes using the database first approach. I have a an EDMX file in my solution.

I am trying to add attributes to my classes using the MetadataTypeAttribute approach which seems to be the recommended way of doing this outside of editing the T4 template, however, I cannot seem to get it to work because I keep getting this compile error:

'Patient' is an ambiguous reference between 'PatientManagementSystem.Patient' and 'PatientManagementSystem.Models.Patient'

Here is the code I am using:

[MetadataTypeAttribute(typeof(PatientMetadata))]
public partial class Patient
{
}

public class PatientMetadata
{

    [Required]
    public string LastName {get; set;}

}

Is this error happening because I don't actually have classes for this because I am doing Database first and everything in the EDMX?

Thanks! Flea

like image 653
Flea Avatar asked Apr 27 '13 21:04

Flea


1 Answers

Make sure both are in the same namespace. Since it's a partial class, the class you are modifying must also be in the same namespace or it isn't the same class.

like image 185
Adam Tuliper Avatar answered Oct 22 '22 10:10

Adam Tuliper