1

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; }
Evonet
  • 3,600
  • 4
  • 37
  • 83
  • Are you sure that is an MVC problem? MVC does not deal directly with tables and columns, as far as I know. – Kobi May 21 '13 at 11:24
  • An Entity Framework problem, the error occurs when running update-database in the package manager console – Evonet May 21 '13 at 11:27
  • Does it have to be a byte? Why not just use a Nullable – KingCronus May 21 '13 at 12:03
  • Why are You sure that your property "can be null"? – Kamil Będkowski May 21 '13 at 12:05
  • @KingCronus - The property is used to store an image in the database, can I do the same with a smallint? – Evonet May 21 '13 at 12:09
  • @Kamil Well, I haven't set the Required attribute, my assumption was that in CodeFirst, all properties can be null unless primary/foreign keys, or the [Required] DataAnnotation is set – Evonet May 21 '13 at 12:10

4 Answers4

2
[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.

KingCronus
  • 4,509
  • 1
  • 24
  • 49
2

Change it from byte[] to byte?[]

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Himanshu
  • 21
  • 1
1

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.

Kamil Będkowski
  • 1,092
  • 4
  • 16
  • 36
0

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

Community
  • 1
  • 1
pwae
  • 575
  • 5
  • 4
  • 1
    A nullable byte is just not the same than an array of bytes. – ken2k May 21 '13 at 11:50
  • Correct, but from my knowledge a byte array cannot be nullable in Entity Framework. So the solution as far as i can see it, is to declare it as a nullable byte, and handle the db manually. (byte[] is not a nullable type in CLR) – pwae May 21 '13 at 12:09