How can I make my string property nullable?

前端 未结 9 999
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 00:08

I want to make the Middle Name of person optional. I have been using C#.net code first approach. For integer data type its easy just by using ? operator to make

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 00:38

    As others have pointed out, string is always nullable in C#. I suspect you are asking the question because you are not able to leave the middle name as null or blank? I suspect the problem is with your validation attributes, most likely the RegEx. I'm not able to fully parse RegEx in my head but I think your RegEx insists on the first character being present. I could be wrong - RegEx is hard. In any case, try commenting out your validation attributes and see if it works, then add them back in one at a time.

提交回复
热议问题