What is the data annotation for changing nullable and not null data types?

匆匆过客 提交于 2019-12-11 03:42:43

问题


I reckon this should be simple for experienced programmers but here it goes. I am working on a project using entity framework code first.I have also enabled migrations and set to auto (Lovely feature).

I stupidly declared one of the datatype wrong in my entity class and now i realised that it wouldn't work with what i was trying to do. Must have been the auto complete feature. But anyways, the field was nullable and now that i have changed it to what i want, it has set the field to "not null".

Originally: public virtual Byte[] ImageData { get; set; }

Changed to: public virtual byte ImageData { get; set; }

Now that i have changed and built the solution, update-database -force will not work and throws an error that:

Cannot insert the value NULL into column 'ImageData', column does not allow nulls. UPDATE fails. The statement has been terminated.

Is there a Data Annotation i could use to set this field back to nullable?

eg: [DataType(DataType.Upload)]

I have looked through here for a considerable amount of time and i can't seem to find what i am looking for.


回答1:


Change the property type to the nullable form byte?.

public virtual byte? ImageData { get; set; }


来源:https://stackoverflow.com/questions/12995349/what-is-the-data-annotation-for-changing-nullable-and-not-null-data-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!