如何通过单元测试检查的属性标记为在ORM模型计算的? [英] How check by unit test that properties mark as computed in ORM model?

查看:170
本文介绍了如何通过单元测试检查的属性标记为在ORM模型计算的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被实体框架创建ORM 5.0(C#4.5) - 数据库第一个



作为计算即时标志着实体的某些属性(绑定到柱。 。缺省值)



如何通过单元测试检查的属性标记为在ORM模型计算



请注意:测试。需要在ORM紧急重新创建实体后,控制计算性能



实体描述的.edmx *:

 <的EntityType名称=用户> 
<关键>
< PropertyRef名称=标识符/>
< / Key与GT;
<属性名称=标识符TYPE =BIGINT可空=假
StoreGeneratedPattern =身份/>
<属性名称=名称TYPE =nvarchar的可空=假MAXLENGTH =32/>
<属性名=PasswordHashCode1TYPE =INT可空=假/>
<属性名=PasswordHashCode2TYPE =INT可空=假/>
<属性名=CreateDateTimeTYPE =DATETIME2可空=假
StoreGeneratedPattern =计算/>
< /&的EntityType GT;


解决方案

我不知道这是否适用于你的情况 - ,但如果你想读在运行时 - 从的EntityFramework模型 您可以尝试在我先前的职位(由OP,进一步完善)这里所说的一些东西......



我如何编程读取EF的DbContext元数据?



这约的DbContext 会谈(你可以从任何一面工作,这样也适用于你) - 但具体而言,刚拿到 ObjectContext的 - 并继续从这点......

  VAR容器= objectContext.MetadataWorkspace.GetEntityContainer( objectContext.DefaultContainerName,DataSpace.CSpace); 

//,只是让你开始...
VAR baseset = ObjectContext的
.MetadataWorkspace
.GetEntityContainer(objectContext.DefaultContainerName,DataSpace.CSpace)
.BaseEntitySets
。首先(元=> meta.ElementType.Name ==MyBaseClass);

VAR元素类型= ObjectContext的
.MetadataWorkspace
.GetEntityContainer(objectContext.DefaultContainerName,DataSpace.CSpace)
.BaseEntitySets
。首先(元=> meta.ElementType.Name ==MyBaseClass)
.ElementType;

EdmMember成员= elementType.Members [2]; //例如第三个属性
Facet项目;
如果(member.TypeUsage.Facets.TryGetValue(StoreGeneratedPattern,假的,出项))
{
VAR值=((StoreGeneratedPattern)item.Value)== StoreGeneratedPattern.Computed;
}

您获得的 MetadataWorkspace ,你可以从那里工作,你一路下滑。



我们设法提取导航属性等 - 但有可能是每个属性的其他一些信息 - 比如计算 。我还没有尝试过,但它可能的帮助。



另外,我还没有对模型或数据库尝试这第一个 - 但我不明白为什么它不应该工作 - 基础架构是相同的(EF,没有代码先)。



编辑:我添加了一个更具体的代码,让你开始(参见编辑的代码)。那种作品(让你所在的'面'存储),还没有准备好使用的代码,还需要更多的工作。


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>

解决方案

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.

这篇关于如何通过单元测试检查的属性标记为在ORM模型计算的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆