0

我想知道 EF 5 或更新版本中是否有一些关于数据注释的新功能。我正在做数据库优先,所以据我所知,我必须一直操作 T4,这非常混乱,因为每个 EF 版本 MS 都会更改默认的 T4。所以有人可以告诉我,如果有一些替代这样的事情:

操纵T4:

<#=codeStringGenerator.UsingDirectives(inHeader: false, includeAnnotations: true)#>
[MetadataType(typeof(<#=code.Escape(entity)#>Metadata))]
<#=codeStringGenerator.EntityClassOpening(entity)#>

如果类名为“地址”,则生成的类示例:

[MetadataType(typeof(AddressMetadata))]
public partial class Address
{

与生成的“地址”类相同的命名空间中的注释附加类:

public class AddressMetadata
{

    [Display(Name = "Straße")]
    public string Street;

    [Display(Name = "Land")]
    public string Country;

    [Display(Name = "PLZ")]
    public string Zip;

    [Display(Name = "Stadt")]
    public string City;
}

这就是我今天做的方式。

4

1 回答 1

1

您无需更改生成的代码。您可以将伙伴类添加到部分类。然后,当重新生成代码时,您不会丢失任何东西。

于 2012-11-17T12:27:43.453 回答