Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

c# - How check by unit test that properties mark as computed in ORM model?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...