asp.net mvc - MVC Model How to make byte[] nullable? -
i'm using asp.net mvc ef code first.
in model have property can null:
public byte[] avatar { get; set; }
however, when run update-database get:
cannot insert value null column 'avatar', table 'temp'; column not allow nulls. update fails.
i don't have dataannotation specifying property required, nor property foreign key.
any ideas?
further updates:
if delete database , force new 1 created 'update-database -verbose' can see table being created forces not null flag on field:
create table [dbo].[userprofile] ( [userid] [int] not null identity, [username] [nvarchar](max), [firstname] [nvarchar](50) not null, [lastname] [nvarchar](50) not null, [emailaddress] [nvarchar](50) not null, [workphone] [nvarchar](20), [mobilephone] [nvarchar](20), [hiredate] [datetime], [avatar] [image] not null, [avatarmimetype] [nvarchar](max), constraint [pk_dbo.userprofile] primary key ([userid]) )
my full model:
[key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int userid { get; set; } public string username { get; set; } [required] [display(name = "first name")] [maxlength(50, errormessage = "first name cannot longer 50 characters.")] public string firstname { get; set; } [required] [display(name = "last name")] [maxlength(50, errormessage = "last name cannot longer 50 characters.")] public string lastname { get; set; } [display(name = "full name")] public string fullname { { return firstname + " " + lastname; } } [required] [regularexpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$", errormessage = "not valid email address")] [display(name = "email address")] [maxlength(50, errormessage = "email address cannot longer 50 characters.")] public string emailaddress { get; set; } [display(name = "work phone")] [maxlength(20, errormessage = "work phone cannot longer 20 characters.")] public string workphone { get; set; } [display(name = "mobile phone")] [maxlength(20, errormessage = "mobile phone cannot longer 20 characters.")] public string mobilephone { get; set; } [display(name = "hire date")] [datatype(datatype.date), displayformat(dataformatstring = "{0:dd/mm/yyyy}", applyformatineditmode = true)] public datetime? hiredate { get; set; } [validatefile(errormessage = "please select png, jpg or gif image smaller 2mb")] [column(typename = "image")] public byte[] avatar { get; set; } public string avatarmimetype { get; set; }
[column(typename = "image")] public byte[] avatar { get; set; }
i think need tell ef using in fashion. try using image annotation above.
this make sure field created image type; designed type of scenario.
edit: please see comments answer worked.
Comments
Post a Comment