0

I've seen sample code for reading an Entity Framework EdmProperty's StoreGeneratedPattern property (computed, identity, or none) in at least 2 places (here & here), but it doesn't work with my model. My context is an ObjectContext, the version is EF5; maybe this code broke with EF5? When I look at the properties for this property/column in the model, it shows identity.

Here is my code:

using ( var context = new MyApplicationEntities() )
{
    var entityType = ( (EntityConnection)context.Connection )
        .GetMetadataWorkspace() // can't call context.MetadataWorkspace - storage model will not be present
        .GetType( "MyEntityTypeWithIdentityColumn", "MyApplicationModel.Store", DataSpace.SSpace ) as EntityType;

    EdmMember identityColumn = entityType.Members["MyIdentityColumn"];

    Facet item;
    // All I get here for Facets is Nullable & DefaultValue
    if ( identityColumn.TypeUsage.Facets.TryGetValue( "StoreGeneratedPattern", false, out item ) )
    {
        var value = ( (StoreGeneratedPattern)item.Value ) == StoreGeneratedPattern.Identity;
    }
}
Community
  • 1
  • 1
dudeNumber4
  • 4,217
  • 7
  • 40
  • 57

1 Answers1

0

Pawel's question led me to examine the edmx and discover the answer. I said in my question that I could look at the model (UI) and see that the column in question was an identity column. In fact, it was not. In any case I expected the StoreGeneratedPattern property to exist since the enumeration contains all possible values (Identity, Computed, None).

My actual goal was to detect all entities whose primary key column/property was not identity.

I discovered through subsequent testing that the StoreGeneratedPattern property only exists for identity & computed; if the column is not store generated, it doesn't exist (StoreGeneratedPattern.None is never referenced). Thus, the sample code above is fine as long as it's modified to handle the absence of the property (which seems strange to me).

dudeNumber4
  • 4,217
  • 7
  • 40
  • 57