How to define a custom naming convention if EF 5

若如初见. 提交于 2019-12-05 10:45:09
marc_s

By default, a database-first approach will always map a table name to an entity of the same name (possibly pluralized/singularized). There is currently no provision in EF to tweak that by conventions or tooling. You can change/adapt/customize your model - but if you regenerate it, those changes are lost. I'm not aware of any tools/scripts/hacks to somehow "preserve" those changes and re-apply them after regenerating the model from the database.

If you need to extend the generated I'd suggest using the fact that those are partial classes - you can extend the generated class in a second physical file :

Write this in a separate file, e.g. ProductExtension.cs:

public partial class Product
{
   // add your custom methods etc. here
}

Those various files that make up that class will be merged together into one class by the C# compiler for you.

If you need to add data annotations to existing generated classes, you can use the MetadataType(..) attribute as shown in this SO question and its answers:

Write this in a separate file, e.g. ProductExtension.cs:

[MetadataType(typeof(ProductMetaData))]
public partial class Product
{
}

and then define the metadata/data annotations for your products class in yet another file, ProductMetadata.cs, like this:

public class ProductMetaData
{
    [Required]
    public int RequestId {get;set;}
    //...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!