3

如果db 列是主键而不是标识,我想在我的类属性中添加以下属性 考虑以下代码

[Key, Column(Order =  0), DatabaseGenerated (DatabaseGeneratedOption.None )]
public virtual int TypeId
{ get; set; }

我使用以下代码检查主键

bool isPrimaryKey = ef.IsKey(edmProperty);
#>
<#
if (isPrimaryKey)
{
#>
[Key]
<#
}
#>

现在我需要一些代码来检查列是身份吗?如果没有,那么我将从我的 T4 模板代码中添加以下属性
DatabaseGenerated (DatabaseGeneratedOption.None ) 。我正在使用EF 4.x POCO Entity Generator for C#来生成我的 poco 类。现在我想修改它。现在我该怎么做?等待您的帮助。

4

2 回答 2

0

如果您对主键使用 tblWidget 和 WidgetId 的命名约定,那么您可以使用它来检测它是否是主键。

于 2012-08-12T04:49:46.893 回答
0
public const string annotationNamespace = "http://schemas.microsoft.com/ado/2009/02/edm/annotation";

MetadataProperty storeGeneratedPatternProperty = null;
edmProperty.MetadataProperties.TryGetValue(annotationNamespace + ":StoreGeneratedPattern", false, out storeGeneratedPatternProperty);
bool IsIdentity = storeGeneratedPatternProperty != null && storeGeneratedPatternProperty.Value.ToString() == "Identity";
于 2014-01-07T20:58:49.037 回答