Update Model From Database (Database First)

前端 未结 4 1017
暖寄归人
暖寄归人 2020-12-16 06:08

I\'m using MVC3 VS2010 with EF4.1, I have created my DB using SQL Server and I import it to the MVC3 Web Application.

I have a challenge here, when I come to Update

4条回答
  •  有刺的猬
    2020-12-16 07:07

    No, the files will be regenerated every time.

    All the classes are defined as partial so you can easily add DataAnnotations using the MetadataTypeAttribute.

    Let's say you have a User class defined as follow:

    public partial class User {
        public string Name {get;set;}
    }
    

    Create a IUser interface

    public interface IUser {
       [Required]
       [DisplayName("User name")]
       string Name {get;set;}
    }
    

    And then extend the User class to specify that IUser will be used as metadata.

    [MetadataType(typeof(IUser))]
    public partial class User {} //Empty class body
    

提交回复
热议问题