0

I'm trying to add an image to one of my table's in my database but then got the exception: 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll

This is were the object that going in to the table is created, the image is a byte[] just as in it's model (generated by ado code first with existing database , in the database it's a varbinary(max)),

List<ValidationResult> result = _auctionController.Create(
            new Auction
            {
                ProductId = chosenProduct.Id,
                StartTime = dateTimePickerStart.Value,
                EndTime = dateTimePickerEnd.Value,
                Image = image
            });

This is the method where the validation and saving is happening,

    public List<ValidationResult> Create(Auction auction)
    {
        ValidationContext context = new ValidationContext(auction, null, null);
        List<ValidationResult> result = new List<ValidationResult>();
        bool valid = Validator.TryValidateObject(auction, context, result, true);

        if (valid)
        {
            _dbContext.Auctions.Add(auction);
            _dbContext.SaveChanges();
        }

        return result;
    }

The Auction comes through as valid but at the .SaveChanges(); I get the error stated above.

I don't know but I've done wrong and can't find anything about adding images to SQL server database using System.ComponentModel.DataAnnotations when searching for it on google and I really would like to keep my validation.

Before adding the image to the table and model the same code ran with out any errors.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",       "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Auction()
        {
            CustomerAuction = new HashSet<CustomerAuction>();
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ProductId { get; set; }

        [Column(TypeName = "smalldatetime")]
        public DateTime StartTime { get; set; }

        [Column(TypeName = "smalldatetime")]
        public DateTime EndTime { get; set; }

        public byte[] Image { get; set; }

        public virtual Product Product { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<CustomerAuction> CustomerAuction { get; set; }

The InnerExceptions out of what I can find: '((System.RuntimeType)context.ObjectType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException' + GenericParameterAttributes '((System.RuntimeType)context.ObjectType).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException' System.Reflection.GenericParameterAttributes {System.InvalidOperationException} + GenericParameterPosition '((System.RuntimeType)context.ObjectType).GenericParameterPosition' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}

I would like to know why this happens and how to solve it?

Masoud
  • 8,020
  • 12
  • 62
  • 123
Perly X
  • 45
  • 1
  • 7

0 Answers0