问题
I'm created ORM by Entity Framework 5.0 (C# 4.5) - database first.
Some properties of entities i'm marked as computed (binded to columns with defaults).
How check by unit test that properties mark as computed in ORM model?
Note: test need for control computed properties after emergency recreate entity in ORM.
Entity description in *.edmx:
<EntityType Name="Users">
<Key>
<PropertyRef Name="Identifier" />
</Key>
<Property Name="Identifier" Type="bigint" Nullable="false"
StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="32" />
<Property Name="PasswordHashCode1" Type="int" Nullable="false" />
<Property Name="PasswordHashCode2" Type="int" Nullable="false" />
<Property Name="CreateDateTime" Type="datetime2" Nullable="false"
StoreGeneratedPattern="Computed" />
</EntityType>
回答1:
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.
来源:https://stackoverflow.com/questions/15866272/how-check-by-unit-test-that-properties-mark-as-computed-in-orm-model