1

Here is one of my Entityclasses:

public class Customer
{
    public int Id { get; set; }
    [Required]
    public int CustomerNumber { get; set; }
    [Required]
    [StringLength(50)]
    public string Prename { get; set; }
    [StringLength(50)]
    public string Surname { get; set; }
    public DateTime? Birthday { get; set; }
    public bool Active { get; set; }
    [Column(TypeName = "image")]
    public byte[] Image { get; set; }
}

After adding objects, I used the following line to save my changes:

((IObjectContextAdapter)context).ObjectContext.SaveChanges();

Everything worked fine, I was able to save a Customer..

Well now I must change that to the following:

context.SaveChanges();

I found out, that my previous line didn't validate, only the second does, because its inheriting from DbContext

Now I have the problem that I cannot save a Customer anymore, the reason is, that I got an exception at the context.SaveChanges(); Line.

In the EntityValidationError I found the following Error: ErrorMessage = "The field Image must be a string or array type with a maximum length of '4000'." Well know my question is, how can I avoid validating this one entity only..? I know that there is a possibilty to avoid validating a whole entity, but thats not the idea. I hope someone can help me, I'm stuck at this one..

eMi
  • 5,540
  • 10
  • 60
  • 109
  • an Array of bytes, the Image itselfs, no paths.. – eMi Jun 12 '12 at 12:39
  • @eMi Why would you like to avoid data validation? If your DB doesn't accept the operation as the table can't store the value, what's the point? – ken2k Jun 12 '12 at 12:42
  • Becuase it seems that sql-server-ce has issues to save images in the Database.. Take a look at this question: http://stackoverflow.com/questions/5737733/error-storing-image-in-sql-ce-4-0-with-asp-net-mvc-3-and-entity-framework-4-1-co The problem is, I dont want to avoid validation for the whole entity, like it is described there. – eMi Jun 12 '12 at 12:45

2 Answers2

1

You could use a CustomValidationAttribute. Here is an example.

You could make a method that just returns success.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
0

You can also disable validation by setting the ValidateOnSaveEnabled configuration property on the context to false:

Context.Configuration.ValidateOnSaveEnabled = false;
RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41