问题
I'm using ASP.NET MVC with EF code first.
In my model I have a property that can be null:
public byte[] Avatar { get; set; }
However, when I run update-database I get:
Cannot insert the value NULL into column 'Avatar', table 'temp'; column does not allow nulls. UPDATE fails.
I don't have a dataannotation specifying the property to be required, nor is the property a foreign key.
Any ideas?
Further updates:
If I delete my database and force a new one to be created with 'update-database -verbose' then I can see the table being created specifically forces a NOT NULL flag on my 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 be longer than 50 characters.")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
[MaxLength(50, ErrorMessage = "Last Name cannot be longer than 50 characters.")]
public string LastName { get; set; }
[Display(Name = "Full Name")]
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
[Required]
[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$", ErrorMessage = "Not a valid email address")]
[Display(Name = "Email Address")]
[MaxLength(50, ErrorMessage = "Email Address cannot be longer than 50 characters.")]
public string EmailAddress { get; set; }
[Display(Name = "Work Phone")]
[MaxLength(20, ErrorMessage = "Work Phone cannot be longer than 20 characters.")]
public string WorkPhone { get; set; }
[Display(Name = "Mobile Phone")]
[MaxLength(20, ErrorMessage = "Mobile Phone cannot be longer than 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 a PNG, JPG or GIF image smaller than 2MB")]
[Column(TypeName = "image")]
public byte[] Avatar { get; set; }
public string AvatarMimeType { get; set; }
回答1:
[Column(TypeName = "image")]
public byte[] Avatar { get; set; }
I think you need to tell EF that you are using it in this fashion. Try using the image annotation as above.
This will make sure your field is created as the image type; designed for this type of scenario.
EDIT: Please see the comments for the answer that eventually worked.
回答2:
It isn't maybe direct answer but useful workaround. When you are creating entity,initalize Avatar field:
public void CreateSomething(Something something)
{
something.Avatar= new byte[0],
Db.Somethings.Add(something);
Db.SaveChanges();
}
For EF Avatar now is not null so everything is ok. Hope,it will help.
回答3:
Try the solution noted here: What is the data annotation for changing nullable and not null data types?
I.E:
Change it from byte[] to byte?
Cheers
来源:https://stackoverflow.com/questions/16668756/mvc-model-how-to-make-byte-nullable