How can set a default value constraint with Entity Framework 6 Code First?

前端 未结 5 1973
梦毁少年i
梦毁少年i 2020-12-03 01:02

In a legacy app, most string properties can\'t be null and need to have a default value of string.empty.

I know it\'s possible to do this with migrations, but I\'m l

5条回答
  •  爱一瞬间的悲伤
    2020-12-03 01:19

    You can set the default value within the constructor

    class Foo : Model
    {
        public bool DefaultBool { get; set; }
    
        public Foo()
        {
            DefaultBool = true;
        }
    }
    

    When you create a new Foo it will set the true value for the property DefaultBool. This is not a default constraint but it works.

提交回复
热议问题