在运行时获取实体构面和其他元数据 [英] Get entity facets and other metadata on runtime

查看:102
本文介绍了在运行时获取实体构面和其他元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有.NET 4.0 WinForms应用程序,并且我将实体框架5"与模型优先"方法一起使用.在VS EF Designer中,我创建了十几个具有很多String类型标量属性的实体,然后在属性工具栏"中配置了参数(即 General 参数, Facets 参数),以符合数据库要求.

I have .NET 4.0 WinForms Application, and I use Entity Framework 5 with Model First Approach. In VS EF Designer, I have created a dozen or so entities with a lot of scalar properties of String type, then in Properties Toolbar I have configured parameters (i.e. General parameters, Facets Parameters) for them, to fit DB requirements.

在BL层中,我能够验证实体对象以检查其是否包含正确的值,例如使用DbContext.Entry(Of T)(entity).GetValidationResult()方法.但是我还需要为WinForms开发GUI层输入字段验证.我想基于实体集属性的元数据值实现动态GUI验证,以使BL验证与GUI验证同步,并避免冗余的源代码.

In BL layer I am able to validate entity object in purpose to check whether it contains correct values, for example by using DbContext.Entry(Of T)(entity).GetValidationResult() method. But I need to develop also GUI layer input fields validation for WinForms. I would like to implement dynamic GUI validation, based on metadata values of entity set properties, to have BL validation synchronized with GUI validation, and to avoid redundancy of source code written.

我的问题是:如何获取以下内容的元数据值,尤其是构面元数据值(即固定长度最大长度可空)在运行时自动生成的实体?

My question is: How can I get metadata values, particularly facets metadata values (i.e. Fixed Length, Max Length, Nullable) of auto-generated entities on runtime?

据我所知,有可能利用基于手动创建的部分类中的属性属性的数据注释.但是,在模型优先"方法中,此解决方案还可能涉及VS EF设计器属性工具栏和数据库中元数据的冗余问题和同步问题.

As I know, there is a possibility to take advantage of data annotations based on properties attributes in manually created partial class. However, in Model First approach, this solution may also involve redundancy issues and synchronization problems with metadata from VS EF Designer Properties Toolbar and Database.

推荐答案

这应该可以帮助您入门,但是您需要使用调试器并进行专门的测试才能获得所需的内容...

This should help you get started, but you'd need to get to debugger and test specifically to get what you need...

示例代码...

using (var db = new MyContext())
{
var objectContext = ((IObjectContextAdapter)db).ObjectContext;

var baseset = objectContext
    .MetadataWorkspace
    .GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
    .BaseEntitySets
    .First(meta => meta.ElementType.Name == "YourEntityClassName");

var elementType = objectContext
    .MetadataWorkspace
    .GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
    .BaseEntitySets
    .First(meta => meta.ElementType.Name == "YourEntityClassName")
    .ElementType;

EdmMember member = elementType.Members[2];

Facet item;
// if (member.TypeUsage.Facets.TryGetValue(EdmProviderManifest.StoreGeneratedPatternFacetName, false, out item))
if (member.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out item))
{
    var value = ((StoreGeneratedPattern)item.Value) == StoreGeneratedPattern.Computed;
}

但这只是故事的一部分.

But that's just part of the story.

我意识到这在某些情况下是可行的(因此您需要进行一些试验),具体取决于您的需求.但您也有other spaces在其中-例如SSpace.所以对于表名,效果更好...

What I realized is that's working in some cases (thus you need to experiment a bit) depending on what you need. But you also have other spaces in there - e.g. the SSpace. So e.g. for table names this works better...

var ssSpaceSet = objectContext.MetadataWorkspace.GetItems<EntityContainer>(DataSpace.SSpace).First()
    .BaseEntitySets
    .First(meta => meta.ElementType.Name == "YourTableName");

...然后是私有Table属性.

...and then private Table property.

在您的情况下,您应该在其中找到大多数信息-但例如上面生成的商店没有在此处填充-我猜是在其他一些空间"中(更多在该链接之一中).

In your case you should most info in there - but e.g. the above store generated is not populated there - but in some other 'space' I guess (more in one of the links on that).

并查看以下链接:


获取模型架构使用不支持CreateDatabase的提供程序以编程方式创建数据库
如何以编程方式读取EF DbContext元数据?
如何检查通过单元测试确定属性标记为在ORM模型中计算出来的?

And take a look at the following links:


Get Model schema to programmatically create database using a provider that doesn't support CreateDatabase
How I can read EF DbContext metadata programmatically?
How check by unit test that properties mark as computed in ORM model?

这篇关于在运行时获取实体构面和其他元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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