none
How do I identify Enum Type from EdmMember? RRS feed

  • Question

  •   I cannot tell the difference between an Enum EdmMember and Int32 EdmMember in the Entity Framework.  Is there a way to do this?

    Example of what I have been trying:

    ((PrimitiveType)edmMember.TypeUsage.EdmType).PrimitiveTypeKind

    But this returns Int32 for Enum Edm Members.

    Friday, January 31, 2014 7:42 PM

Answers

  • >>What I cannot figure out how to do is tell if the EdmMember is an EdmEnumType or some other type?

    Sorry for misunderstanding you.

    >>It would be great to have a method off of EdmMember that is IsEnumEdmType() or ,alternatively, if I could call GetType() off of the EdmMember and it would return

    The GetType() would work like below:

    foreach (EdmMember edmMember in EdmMembers)
                    {
                        var type = edmMember.TypeUsage.EdmType.GetType();
                    }

    If it is an enum type, it would return System.Data.Metadata.Edm.EnumType value. For non enum type, it it would retutn System.Data.Metadata.Edm.PrimitiveType value.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Catequil Wednesday, February 5, 2014 7:17 PM
    Wednesday, February 5, 2014 6:39 AM
    Moderator

All replies

  • Hello,

    >>Is there a way to do this?

    Yes, there is one workaround, however it may do be like similar with what you have tried.

    For check whether it is the enum type, we need to use BuiltInTypeKind like below:

    MetadataWorkspace MetadataWorkspace = ((IObjectContextAdapter)db).ObjectContext.MetadataWorkspace;
    
    
                    ReadOnlyCollection<EnumType> EnumTypes = MetadataWorkspace.GetItems<EnumType>(DataSpace.CSpace);
    
    
                    // Iterate through the collection to get each complex type.
    
                    foreach (EnumType EnumType in EnumTypes)
    
                    {
    
                        Console.WriteLine("EnumType Name: {0}, Namespace: {1}", EnumType.Name, EnumType.BuiltInTypeKind);
    
                    }
    

    Finding out all the enum types in metadata and then we can do a comparative operation to check whether it is a enum type.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 3, 2014 4:57 AM
    Moderator
  • So how would I get the enum type from the edm member doing this? I only see that you are getting all the Enum Types. But what I need to do is get an EdmMember by name, which I can already do. What I cannot figure out how to do is tell if the EdmMember is an EdmEnumType or some other type? It would be great to have a method off of EdmMember that is IsEnumEdmType() or ,alternatively, if I could call GetType() off of the EdmMember and it would return EnumEdmType.   One of these would solve my problem. I hope this clarifies what I am looking for.

    Thanks in advance!


    • Edited by Catequil Monday, February 3, 2014 8:44 PM clarification
    Monday, February 3, 2014 8:41 PM
  • >> It would be great to have a method off of EdmMember that is IsEnumEdmType() or ,alternatively, if I could call GetType() off of the EdmMember and it would return EnumEdmType.

    There is no such method. Since we can get all the enum types what Fred has done and you can get the edmMember name, the enum types has its own name, too, we just need to compare their names to check if the edmMember is in enum type list.

    Tuesday, February 4, 2014 9:16 AM
  • Ok, I can kind of see where you might be going with this. Where I am not sure is this: I have the EdmMember Name and I have the EnumType Name, but these do not match and they don't have too.  They are based on whatever the programmer at the time called them.  So is there a way to get the EnumType Name from the EdmMember to compare?

    Background:

    The EdmMember Enums were created by doing a scalar property of Type Int32 and Converting them to Type Enum using VS 2012/13.  I'm saying this because he comments in his code "Complex Type".  Is there a way to create an Enum Type using a Complex Type vs Scalar Type?

    Maybe I am asking the wrong question.  Is there a way to get the Selectable Enumerations that a particular EdmMember of EnumType would use in the code behind?  There must be a way to do this because they would have to do it for when an EdmMember is bound to an asp:DropDownList.


    • Edited by Catequil Tuesday, February 4, 2014 8:47 PM
    Tuesday, February 4, 2014 7:58 PM
  • How do you get the EdmMember object, could you please share it?
    Wednesday, February 5, 2014 2:52 AM
  • >>What I cannot figure out how to do is tell if the EdmMember is an EdmEnumType or some other type?

    Sorry for misunderstanding you.

    >>It would be great to have a method off of EdmMember that is IsEnumEdmType() or ,alternatively, if I could call GetType() off of the EdmMember and it would return

    The GetType() would work like below:

    foreach (EdmMember edmMember in EdmMembers)
                    {
                        var type = edmMember.TypeUsage.EdmType.GetType();
                    }

    If it is an enum type, it would return System.Data.Metadata.Edm.EnumType value. For non enum type, it it would retutn System.Data.Metadata.Edm.PrimitiveType value.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Catequil Wednesday, February 5, 2014 7:17 PM
    Wednesday, February 5, 2014 6:39 AM
    Moderator
  • Here is the methods I used to get all Members of an EntityType and then I included the method to refine it to one by name.  The CAll would look something like:

    GetMember("Students", "LastName"); 

    public EdmMember GetMember(String entityCollectionName, String memberName)
            {
                foreach (EdmMember member in GetMembers(entityCollectionName))
                {
                    if (member.Name.Equals(memberName))
                    {
                        return member;
                    }
                }
                return null;
            }
    
            public IEnumerable<EdmMember> GetMembers(String entityCollectionName)
            {
                ReadOnlyMetadataCollection<EdmMember> rval = null;
                ObjectContext objContext = ((IObjectContextAdapter)this).ObjectContext;
                MetadataWorkspace ws = objContext.MetadataWorkspace;
                EntitySetBase entitySetBase = ws.GetEntityContainer(objContext.DefaultContainerName, DataSpace.CSpace).BaseEntitySets.First(b => b.Name == entityCollectionName);
                rval = entitySetBase.ElementType.Members;
                return rval;
            }



    • Edited by Catequil Wednesday, February 5, 2014 7:25 PM
    Wednesday, February 5, 2014 6:01 PM
  • This worked after changing the DataSpace for the EdmMember from SSpace to CSpace.

    Thanks Fred!

     
    • Edited by Catequil Wednesday, February 5, 2014 7:18 PM
    Wednesday, February 5, 2014 7:17 PM