MVC模型如何使byte []可为空? [英] MVC Model How to make byte[] nullable?
问题描述
我首先使用带有EF代码的ASP.NET MVC.
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:
无法将值NULL插入表'temp'的'Avatar'列中; 列不允许为空. UPDATE失败.
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.
有什么想法吗?
进一步更新:
如果我删除数据库并使用'update-database -verbose'强制创建一个新数据库,则可以看到正在创建的表专门在字段上强制使用NOT NULL标志:
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])
)
我的完整模型:
[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; }
推荐答案
[Column(TypeName = "image")]
public byte[] Avatar { get; set; }
我认为您需要告诉EF您正在以这种方式使用它.尝试使用上述图像批注.
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.
编辑:请查看注释以获取最终的答案.
Please see the comments for the answer that eventually worked.
这篇关于MVC模型如何使byte []可为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!