How to inspect EF model metadata at runtime?

后端 未结 1 1765
深忆病人
深忆病人 2020-12-17 06:21

I need to do some trimming before saving various fields in our database. We\'re deserializing xml from another application into EF entities and then inserting them. There ar

相关标签:
1条回答
  • 2020-12-17 06:44

    You can find the properties by this piece of code:

    var varchars = context.MetadataWorkspace.GetItemCollection(DataSpace.CSpace)
        .Where(gi => gi.BuiltInTypeKind == BuiltInTypeKind.EntityType)
        .Cast<EntityType>()
        .SelectMany(entityType => entityType.Properties
            .Where(edmProperty => edmProperty.TypeUsage.EdmType.Name == "String")
            .Where(edmProperty => (int)(edmProperty.TypeUsage.Facets["MaxLength"]
                .Value) >= 4000))
        .ToList();
    

    The trick is to extract the entity types from the model by BuiltInTypeKind.EntityType and cast these to EntityType to get access to the Properties. An EdmProperty has a property DeclaringType which shows to which entity they belong.

    0 讨论(0)
提交回复
热议问题