Setting default value in entity framework - Database First

前端 未结 2 781
再見小時候
再見小時候 2020-12-11 19:53

I am using Entity Framework 4.3.1, with auto-generated entities from the database.

From this, is there any way to set the default value to something? I don\'t want t

相关标签:
2条回答
  • 2020-12-11 20:30

    The example you give means that DESCRIPTION can only ever be "default string"

    You can set it in the constructor

    namespace name.Models 
    {
      public partial class ENTITY
      {
        private string defaultDescription = "some text";
        public ENTITY() {
          DESCRIPTION_ = defaultDescription;
        } 
      }
    }
    

    or by switching your property to one with a backing field

    namespace name.Models
    {
        public partial class ENTITY
        {
            private string _desc = "some default value"; 
            public virtual string DESCRIPTION_ {get {return _desc} set {_desc = value;} }
        }
    }
    
    0 讨论(0)
  • 2020-12-11 20:35

    You use OnCreated on the partial class:

    public partial class ENTITY
    {    
      partial void OnCreated()
      {
        DESCRIPTION_ = "default string";
      } 
    }
    
    0 讨论(0)
提交回复
热议问题