c# - How do I override the get; set; properties of a field? -


i'm working database due reason out of control - cannot modify schema.

this database has field "certificateid" non-nullable, however, field still considered optional. when load field model (i'm using db first approach), of course tagged being non-nullable, can see in designer.cs

[edmscalarpropertyattribute(entitykeyproperty = false, isnullable = false)] [datamemberattribute()] public global::system.string certificateid {         {         return _certificateid;     }     set     {         oncertificateidchanging(value);         reportpropertychanging("certificateid");         _certificateid = structuralobject.setvalidvalue(value, false);         reportpropertychanged("certificateid");         oncertificateidchanged();     } } 

since field optional, gets passed null sometimes, fails validation.

is there way of overloading these properties autogenerated? able check if value passed set property null, , if is, set empty string before goes validation.

or, possible override metadata property , have isnullable set true?

first of if field considered not nullable mustn't set value null. once manually assign null in code it's bug.

another problem have solved default value (null) if don't assign value. discussed in this question - way initializing field constructor.

if reason previous 2 options not want have last choice of modifying value in overriden savechanges. like:

public override int savechanges(saveoptions options) {     var data = context.objectstatemanager                       .getobjectstateentries(entitystate.added | entitystate.modified)                       .where(e => !e.isrelationship)                       .select(e => e.entity)                       .oftype(myentity);      foreach(var entity in data)     {         if (entity.certificateid == null)         {             entity.certificateid = string.empty;         }     }      return base.savechanges(options); } 

Comments