Entity Framework学习 - 5.DB First执行时提示model没有key

原因:自动生成的类中有关联主键,没有自动生成Key及Column

解决方法:在xxx.tt的66行左右修改为

var simpleProperties = typeMapper.GetSimpleProperties(entity);
if (simpleProperties.Any())
{
    var keyIndex = 0; // This is a new line
    foreach (var edmProperty in simpleProperties)
    {
        // The following if block is new
        if (ef.IsKey(edmProperty)) 
        {
#>          [System.ComponentModel.DataAnnotations.Key]
            [System.ComponentModel.DataAnnotations.Schema.Column(Order = <#=keyIndex.ToString()#>)]
<#
            keyIndex++;
#>
<#      }
#>
        <#=codeStringGenerator.Property(edmProperty)#>
<#
    }
}

  

posted @ 2017-10-11 22:08  朋克  阅读(219)  评论(0编辑  收藏  举报