mardi 7 novembre 2017

Using the method outside of Context returns different result EF 6

I have the method, that is placed at DbContext class, which gets the table name from DB depends on type:

 public Type GetDataType(Type type)
        {
                if (!type.IsSubclassOf(typeof(Test)))
                    throw new ArgumentException("The Wrong type provided. A subclass of Test is expected.");

                Type testType = GetEntityType(type);

                TestAttribute testAttribute = testType.GetCustomAttributes(false).OfType<TestAttribute>().SingleOrDefault() as TestAttribute;

                if (testAttribute == null)
                    throw new ArgumentException("Test attribute not found");

                Type result = testAttribute.DataType;         

                return result;             
        }


    public Type GetEntityType(Type type)
    {
        return type.Namespace == "System.Data.Entity.DynamicProxies" ? type.BaseType : type;
    }

What I am trying to do is moving those methods to the BL, but the testAttribute becomes null after, despite the fact that testType.GetCustomAttributes(false) returns the same in two cases.

My test attribute is:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class TestAttribute : Attribute
    {
        public Type DataType { get; private set; }

        public TestAttribute(Type dataType)
        {
            DataType = dataType;
        }
    }

And the class I am working with is:

 [Test(typeof(testDataItem))]
    public class classTest : Test
    {
     //some fields
    }

What am I missing?





Aucun commentaire:

Enregistrer un commentaire