EF 5中的DataAnnotations没有代码优先

时间:2012-11-17 11:06:05

标签: c# entity-framework

我想知道在EF 5或更新版本中是否存在关于数据注释的新功能。我正在做数据库优先,所以据我所知,我必须一直操纵T4,因为每个EF-Version MS都会更改默认的T4。所以有人可以告诉我,如果有类似的东西可以替代:

操纵T4:

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

如果类名为“Address”,则生成类示例:

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

与生成的“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;
}

这就是我今天这样做的方式。

1 个答案:

答案 0 :(得分:1)

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

相关问题