在 POCO 模板 t4 生成器中查找属性是主键 [英] Finding a property is Primary Key in POCO Template t4 generator

查看:32
本文介绍了在 POCO 模板 t4 生成器中查找属性是主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VS 2012 附带的 POCO t4 模板生成器.我做了一些更改以包含 Entity.Name,但我无法找出主键.

I'm using the POCO t4 template generator that comes with VS 2012. I made few changes to include the Entity.Name, but I'm not able to figure out the primary key.

public string EntityClassOpening(EntityType entity)
{
    return string.Format(
        CultureInfo.InvariantCulture,
        "{0} {1}partial class {2}{3}<{4},{5}>{6}",
        Accessibility.ForType(entity),
        _code.SpaceAfter(_code.AbstractOption(entity)),
        _code.Escape(entity),
        ": EntityBase",
        entity.Name,
        entity.Name,
        _code.StringBefore(" ", _typeMapper.GetTypeName(entity.BaseType)));
}

我没有找到从 EntityType 对象层次结构中查找主键的方法.它公开了属性,但该属性没有任何内容表明它是主键.

I don't find a way to find the primary key from the EntityType object hierarchy. It exposes properties but the property does not have anything to say it is a primary key.

感谢任何帮助.

推荐答案

以防万一有人在迁移 RIA 服务时尝试这样做,我在 VS2013 中使用标准 dbcontext 模板并向实体添加了两件事模板.

Just in case anyone is trying to do this while migrating RIA services stuff, I'm using the standard dbcontext template in VS2013 and have added two things to the entities template.

首先你需要:

using System.ComponentModel.DataAnnotations;

我把它放在靠近顶部的//---- 块下面.

I put it just under the //---- block near the top.

然后我修改了看起来像这样的代码.只需搜索名字即可.我的更改是 ef.IsKey... 并添加 Key() 属性.

Then I modified the bit of code that looks like this. Just search for the first name. My change is ef.IsKey... and adding the Key() attribute.

    var simpleProperties = typeMapper.GetSimpleProperties(entity);
    if (simpleProperties.Any())
    {
        foreach (var edmProperty in simpleProperties)
        {
#>
 <#if (ef.IsKey(edmProperty))
   {#>      [Key()]
   <#}#>
    <#=codeStringGenerator.Property(edmProperty)#>
<#
        }
    }

这篇关于在 POCO 模板 t4 生成器中查找属性是主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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