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

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

问题描述

我是由Entity Framework 5.0(C#4.5)创建的ORM。 - 数据库首先。

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).

如何按单位测试检查属性标记是否在ORM模型中计算?

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

注意:测试需要在ORM中紧急重建实体后控制计算属性。

Note: test need for control computed properties after emergency recreate entity in ORM.

* .edmx中的实体描述:

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>


推荐答案

我不知道这是否适用于您的案件 - 但如果您想在运行时读取元数据 - 从 EntityFramework模型 可以尝试在我之前的帖子中提到的一些事情(并由OP进一步改进)...

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)...

我如何以编程方式读取EF DbContext元数据?

谈到 DbContext (你可以从任何一边工作,所以也适用于你) - 但具体来说,只要得到 ObjectContext - 并从此继续...

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;
}

你得到 MetadataWorkspace ,你可以从那里下来。

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.

我还没有在模型或数据库上尝试这个 - 但是我不明白为什么它不起作用 - 基础设施是一样的(EF,不是代码第一)。

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).

编辑:我添加了更具体的代码让您开始(请参阅编辑过的代码)。这种作品(让你在那里存储facet),它不是可以使用的代码,需要更多的工作。

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天全站免登陆