How to prevent from DataAnnotations Attributes being deleted in DataBase First Model

╄→尐↘猪︶ㄣ 提交于 2021-02-07 05:43:04

问题


I have my asp.net mvc 3 application with entity framework and i used the Database First model to set it up.

My Steps below: 1. Genarated a database with tables 2. Created ADO.NET Entity Data Model file (.edmx) and imported the tables 3. inside the design i added a Code Generation item and used ADO.NET DbContext Generator 4. a Model1.tt holder as been made with all of the tables Models

I have edited the models and updated them with DataAnnotations Attributes (just for the example a well known one)

public class LogOnModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

Now when im making changes to the database and updating it to the edmx file all the models will be overwritten and the DataAnnotations Attributes will disapear.

my question: how can i use database first model and still edit the models for speciific validation like im free to do with code first model? (please no third party tools solution) thanks


回答1:


You need to use buddy classes. See my dated but still useful article http://msdn.microsoft.com/en-us/library/ee256141(v=vs.98).aspx




回答2:


Use ViewModels in your Views. This will decouple your EF entities from your UI logic.



来源:https://stackoverflow.com/questions/9230198/how-to-prevent-from-dataannotations-attributes-being-deleted-in-database-first-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!