Search code examples
c#asp.netentity-frameworkbase64savechanges

validation failed for one or more entities Entity Framework in ASP .Net


Exception was when i trying to save converted image to base64 format:

public async Task<HttpResponseMessage> Upload()
{
    if (!Request.Content.IsMimeMultipartContent()) 
    {
        Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
    }

    var provider = GetMultipartProvider();

    var result = await Request.Content.ReadAsMultipartAsync(provider);

    //Get Album name from Form
    var titleOfAlbum = GetTitleOfAlbum(provider);
    //get path to file
    var pathToCoverDecoded = result.FileData.First().LocalFileName;
    //ENCODE IMAGE TO BASE64
    var bMap = new Bitmap(pathToCoverDecoded);
    byte[] imageBtes;
    string base64;
    using (MemoryStream ms = new MemoryStream())
    {
        bMap.Save(ms, bMap.RawFormat);
        imageBtes = ms.ToArray();
        base64 = Convert.ToBase64String(imageBtes);
    }

    Album al = new Album();
    al.Title = titleOfAlbum;
    al.PathToCover = base64;
    db.Albums.Add(al);
    db.SaveChanges();

    return new HttpResponseMessage(HttpStatusCode.OK);
}

Screenshot of exception message

Tell me please why i get this exception and how i can resolve it ?


Solution

  • The field PathToCover must be a string or array type with a maximum length of '4000'