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; }


}
6
  • you can specify in migration code like : stackoverflow.com/questions/20136504/… Commented Jun 20, 2017 at 1:29
  • Hi,i am using Database first method instead. thanks Commented Jun 20, 2017 at 1:33
  • 2
    I have seen this link this link dotnet-concept.com/Tips/2016/1/5798842/… 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. Commented Jun 20, 2017 at 1: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. Commented Jun 20, 2017 at 2: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 Commented Jun 21, 2017 at 1:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.