Finding a property is Primary Key in POCO Template t4 generator

Use EntityType.KeyMembers property to get properties the primary key consists of.


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.

first you need:

using System.ComponentModel.DataAnnotations;

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

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)#>
<#
        }
    }

Tags:

T4