简体   繁体   中英

DataAnnotations in EF 5 without code-first

I'm wondering if there is some new functionallity regarding data annotations in EF 5 or newer. I'm doing database-first so that as far as I know, I have to manipulate the T4 all the time which is quite a mess because with each EF-Version MS changes the default T4. So can someone tell me, if there is some alternative to things like that:

Manipulated T4:

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

Resulting class sample if class is named 'Address':

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

Additional class for annotations in same namespace as generated 'Address'-class:

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;
}

Thats the way I do it today.

You don't need to change the generated code. You can add the buddy class to a partial class. Then, when the code is regenerated, you don't lose anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM