1

I am trying to use a custom attribute on a Entity class generated automatically by the Entity Framework.

The problem is how to add an property attribute on an existing field?

Here the point where I am right now:

// the custom attribute class
public class MyCustomAttribute : Attribute
{
    public String Key { get; set; }
}

// Entity Framework class generated automatically
public partial class EntityClass
{  
    public String Existent { get; set; }
    //...
}

// set a metadata class for my entity
[MetadataType(typeof(EntityClassMetaData))]
public partial class EntityClass
{
    // if I add a new property to the entity, it works. This attribute will be read
    [MyCustomAttribute(Key = "KeyOne" )]
    public int newProp { get; set; }
}

public class EntityClassMetaData
{
    // adding the custom attribute to the existing property
    [MyCustomAttribute(Key = "keyMeta") ]
    public String Existent { get; set; }    
}

Running this test:

[TestMethod]
public void test1()
{
        foreach (var prop in typeof(EntityClass).GetProperties())
        {
            var att = prop.GetCustomAttribute<MyCustomAttribute>();

            if (att != null)
            {
                Console.WriteLine($"Found {att.Key}");
            }
        }                    
}

will produce:

Found KeyOne

Or the Metadata class store the attribute in a different way or only works for data annotations.

I am stuck here, how can I set and read custom attributes of the generated class without having to edit the generated file?

2 Answers 2

2

I came across this same problem today. I figured EF magic would do the trick and map the attribute across to each model property. Turns out it does, but only for EF data annotations and I couldn't find an answered solution to pull out custom attributes so made this function. Hope it helps dude.

private object[] GetMetadataCustomAttributes(Type T, string propName)
    {
        if (Attribute.IsDefined(T, typeof(MetadataTypeAttribute)))
        {
            var metadataClassType = 
            (T.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault() as 
            MetadataTypeAttribute).MetadataClassType;
            var metaDataClassProperty = metadataClassType.GetProperty(propName);
            if (metaDataClassProperty != null)
            {
                return metaDataClassProperty.GetCustomAttributes(true);
            }
        }
        return null;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I believe if you want to set an attribute in the metadata class, you have to use this syntax:

public class EntityClassMetaData
{
    // adding the custom attribute to the existing property
    [MyCustomAttribute(Key = "keyMeta") ]
    public String Existent;  
}

You must not have { get; set; } on your pre-existing property - just the property with the correct name and datatype.

1 Comment

Nope. Tested with that change but got the same result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.