判断一个实体的使用语境实体框架4.0的主键 [英] Determine Primary Keys of an Entity using the Context in Entity Framework 4.0

查看:316
本文介绍了判断一个实体的使用语境实体框架4.0的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的实体框架数据模型生成的几个实体。在.edmx文件,我可以看到被标记为主键的属性。我的POCO的/自定义背景从这个和T4模板生成的。

I have several entities generated from my Entity Framework data model. In the .edmx file I can see the properties that are flagged as primary keys. My POCO's/custom context are generated from this and the T4 templates.

我要寻找一种方式来找出使用反射与实体框架4.0我的实体的主键。是否有我需要设置一个属性就被置我POCO产生时,他们的?是否有一个属性在我的背景,我可以用它来向下钻取,并找到这些信息?

I am looking for a way to find out the primary keys of my entities using reflection with Entity Framework 4.0. Is there an attribute I need to setup to get set on my POCO's when they are generated? Is there a property in my context I can use to drill down and find this information?

推荐答案

更​​新我的T4模板,添加以下内容:

Updated my T4 template to add the following:

<#  if(ef.IsKey(edmProperty))
{
#>
[EdmScalarPropertyAttribute(EntityKeyProperty=<#=code.CreateLiteral(true)#>)]
<#
}
#>

然后使用反射来找到EntityKeys。

Then used reflection to find the EntityKeys.

这篇关于判断一个实体的使用语境实体框架4.0的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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