I have an .EDMX with my models, from EF 6.0, and I want to add attributes to some of my fields. I've read many examples where they use DataAnnotations with MetadataType... I've tried to implement it, but it does not override... For example if I have
[Queryable]
public string Name;
it will not work.
but if I have
[Queryable]
public string Name2;
It will work and I will see Name2 as part of the attributes!
The code I use in order to find those attributes is as follow :
var properties = typeof(TEntity).GetProperties().Where(prop => prop.IsDefined(typeof(QueryableAttribute), false));
Like I said, when I have Name2, i can find it in the attributes list. And when I have Name, I don't ...
here is my 3 files, they are both in "MMS.Entities" namespace
AreaMetadata.cs
namespace MMS.Entities
{
[MetadataType(typeof(AreaMetadata))]
public partial class Area
{}
public class AreaMetadata
{
[Queryable]
public string Name;
[Queryable]
public string Abbreviation;
[Queryable]
public string Description;
}
}
Area.cs
namespace MMS.Entities
{
using MMS.Common.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Area : Entity, IEntity
{
public Area()
{
this.Plants = new HashSet<Plant>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Abbreviation { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted { get; set; }
public int UserCreatedId { get; set; }
public Nullable<int> UserModifiedId { get; set; }
public System.DateTime DateCreated { get; set; }
public Nullable<System.DateTime> DateModified { get; set; }
public virtual ICollection<Plant> Plants { get; set; }
}
}
Should the name of AreaMetadata.cs be different? Should I include anything somewhere in order to make them both work together?
Thanks for your advices!