I've been searching for a way to add attributes to my entities classes properties and the only way I've found of reaching that was like in this example Adding custom property attributes in Entity Framework code
So I applied that to my project and it seemed like it should work well, but when I'm trying to reflect through the property attributes I'm getting null. I've checked my code for multiple times and just can't figure out why does it happens.
example of my code: in entity base model:
public partial class Trainer
{
public Trainer()
{
this.Subjects = new HashSet<Subject>();
}
public int ID { get; set; }
public int PersonalDataID { get; set; }
public virtual PersonalData PersonalData { get; set; }
public virtual ICollection<Subject> Subjects { get; set; }
}
in my partial class:
[MetadataType(typeof(Trainer.TrainerMetadata))]
public partial class Trainer
{
internal class TrainerMetadata
{
[Search("PersonalData", true)]
public virtual PersonalData PersonalData { get; set; }
[Search("Subject", true)]
public virtual ICollection<Subject> Subjects { get; set; }
}
}
code I'm using for reflection:
foreach (PropertyInfo pI in typeof(Trainer).GetProperties())
{
//sAttr always == null
SearchAttribute sAttr = pI.GetCustomAttributes(typeof(SearchAttribute))
.FirstOrDefault() as SearchAttribute;
}
Any help will be appreciated.