I'm not sure if this applies to your case - but if you want to read the metadata
at runtime - from the EntityFramework model
you could try a few things mentioned in my earlier post here (and further improved by the OP)...
How I can read EF DbContext metadata programmatically?
That talks about DbContext
(which you can work with from any side, so that also applies to you) - but specifically, just get the ObjectContext
- and continue from this point...
var container = objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace);
// and just to get you started...
var baseset = objectContext
.MetadataWorkspace
.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets
.First(meta => meta.ElementType.Name == "MyBaseClass");
var elementType = objectContext
.MetadataWorkspace
.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets
.First(meta => meta.ElementType.Name == "MyBaseClass")
.ElementType;
EdmMember member = elementType.Members[2]; // e.g. 3rd property
Facet item;
if (member.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out item))
{
var value = ((StoreGeneratedPattern)item.Value) == StoreGeneratedPattern.Computed;
}
You get the MetadataWorkspace and you can work your way down from there.
We managed to extract navigation properties etc. - but there might be some other info for each property - like calculated
. I haven't tried but it might help.
Also I haven't tried this on the model or database first - but I don't see why it shouldn't work - the infrastructure is the same (EF, not code first).
EDIT: I added a more specific code to get you started (see edited code). That kind of works (gets you where the 'facets' are stored), it isn't ready-to-use code, more work is needed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…