1

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!

  • In the example I provided, I forgot to put { get; set; } in the properties above... but that is not my problem, as I have them already in my code – Hugo Brizard Feb 16 '15 at 04:40
  • Maybe AreaMetadata's properties gets called first and get overriden by the Area.cs properties? Would that be a possibility? If so how can I manage to call the EDMX first and append the Metadata after? – Hugo Brizard Feb 16 '15 at 04:43

1 Answers1

1

I believe a similar question has been answered here.

When you try to access a MetadataType property attribute, you have to get to the MetadataType using reflection, and not simply the class that uses the MetadataType. There is an explanation in the link I provided and an example of how to get it.

Basically, use reflection to get the AreaMetadata properties, not the Area properties.

Try:

    MetadataTypeAttribute metadata = typeof(TEntity)
        .GetCustomAttributes(typeof(MetadataTypeAttribute), true)
        .OfType<MetadataTypeAttribute>().ToArray().FirstOrDefault();    

    PropertyInfo[] properties = metadata.MetadataTypeClass.GetProperties()
        .Where(prop => prop.IsDefined(typeof(Queryable), false));
Community
  • 1
  • 1
Alex
  • 298
  • 1
  • 3
  • 12