How to know whether a property stored in DbEntityEntry is a property`?

c# entity-framework entity-framework-6


In Entity Framework 6 within an DbEntityEntry certain information can be retrieved by calling Property. However, this fails with an ArgumentException when the property is not a property but a collection or reference. Than other functions must be used.

How can I know which function to call? That is, how can I know of what type (simple property, complex property, reference, collection) the property is?

For DbEntityEntry see

I am using Entity Framework 6.1.3 in Visual Studio 2013.

5/8/2015 4:24:26 AM

Accepted Answer

DbEntityEntry.Member(string) returns a DbMemberEntry, which you can check with (memberEntry is DbPropertyEntry).

5/14/2015 12:31:58 AM

Popular Answer

I have find how to get if the navigation property is collection type or not. For this, we need to get BuiltInTypeKind of the property.

I am using this code for getting all navigation properties of the entity:

var entitySetElementType = ((IObjectContextAdapter)context).ObjectContext.CreateObjectSet<TEntity>().EntitySet.ElementType;
var navProperties = entitySetElementType.NavigationProperties;

Then, we can know if th navigation property is collection or not:

foreach (var navigationProperty in entiySetElementType.NavigationProperties)
      var builtInType = navigationProperty.TypeUsage.EdmType.BuiltInTypeKind;
      var isCollection = builtInType == System.Data.Metadata.Edm.BuiltInTypeKind.CollectionKind
                      || builtInType == System.Data.Metadata.Edm.BuiltInTypeKind.CollectionType;


Since EF has been moved to a separate assembly & namespace, the System.Data.Metadata.Edm.BuiltInTypeKind.CollectionKind in above code should be changed to System.Data.Entity.Core.Metadata.Edm.BuiltInTypeKind.CollectionKind.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow