0

Hi is there a cleaner way to set default value in database first other than initialize in the partial class like below?

[MetadataType(typeof(PersonAttribute))]
public partial class person {
    public person() {
        isMale = true;
    }
}


public class PersonAttribute
{
    [Display(Name = "Title")]
    [Required(ErrorMessage = "Please enter the news title.")]
    public string UserTitle { get; set; }
    public bool isMale { get; set; }


}
MVC newbie
  • 579
  • 3
  • 9
  • 26
  • you can specify in migration code like : https://stackoverflow.com/questions/20136504/how-can-set-a-default-value-constraint-with-entity-framework-6-code-first – Ehsan Sajjad Jun 20 '17 at 01:29
  • Hi,i am using Database first method instead. thanks – MVC newbie Jun 20 '17 at 01:33
  • 2
    I have seen this link this link http://dotnet-concept.com/Tips/2016/1/5798842/Inserting-default-values-in-database-while-inserting-record-in-Entity-Framework that states: Step 1: Open EDMX file and locate your table in that file. Step 2: Select the property name. Right click and click on property. Step 3: You will find a property named as StoreGeneratedPattern with value set as None. Step 4: Set StoreGeneratedPattern = "Computed". Now you can insert default values using EF. I have no idea if this works or not though. – Gwasshoppa Jun 20 '17 at 01:48
  • The other thing to check is... To give a default value the property must be non-nullable otherwise you will need to set the default value inside the constructor. – Gwasshoppa Jun 20 '17 at 02:02
  • no luck with the method above.i tried to set the value in database then set to Computed doesnt work. so i set the default value in dataAnnotation also. also no luck when i set default value on both – MVC newbie Jun 21 '17 at 01:50
  • Hi sorry, i tested again. it works after i insert to database.Previously i set a break point during processing and didnt show up. thanks. solved. – MVC newbie Jun 21 '17 at 02:00

0 Answers0