What is the data annotation for changing nullable

2019-07-10 07:27发布

问题:

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; }