Get Identity Field out of KeyMembers

前端 未结 2 1525
名媛妹妹
名媛妹妹 2021-01-21 15:46

I would like to get the KeyMembers where I have set in the Edmx the StoreGeneratedPattern to Identity is there a way to do this?

I

相关标签:
2条回答
  • 2021-01-21 16:23

    From a given MetadataWorkspace, let's call mw, i use this:

    var cSpaceEntities = mw.GetItems(DataSpace.CSpace).OfType<EntityType>();
    
    foreach (var entity in cSpaceEntities) {
         var autoIds = entity.KeyMembers.Where(p => 
                            p.MetadataProperties
                                .Any(m =>    m.PropertyKind == PropertyKind.Extended 
                                      && Convert.ToString(m.Value) == "Identity")).ToArray();
                }
    

    please test

    0 讨论(0)
  • 2021-01-21 16:37

    The store-generated pattern is in the SSDL content of the EF model. Here's an example of how you can get the properties with identity specification:

    var items = oc.MetadataWorkspace.GetItems(DataSpace.SSpace).OfType<EntityType>();
    foreach (var entityType in items)
    {
        var props = string.Join(",", entityType.Properties
                    .Where(x => x.IsStoreGeneratedIdentity));
        Trace.WriteLine(string.Format("{0}: {1}", entityType.Name, props));
    }
    

    (where oc is an ObjectContext)

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