Annotating properties on a model with default values

后端 未结 5 1991
说谎
说谎 2020-12-01 05:40

I created an EF4.1 code-first model (may or may not be important), and I\'m trying to get default values for my Create scaffold template. My model looks like:



        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 06:00

    class Person {
        public Person() {
            Age = 18;    
        }
        public int Age { get; set; }
    }
    

    In this scenario, every time you do a new Person age will be initialized with 18 as age, even when the new object is created by the Model Binder.

提交回复
热议问题