What is the best way to save multiple image links in a database?

0
List<String> _lista = new List<String>();
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Veiculo veiculo, HttpPostedFileBase[] file)
    {

            for (int i = 0; i < file.Length; i++)
            {
                var fileName = Guid.NewGuid().ToString() +
                 System.IO.Path.GetExtension(file[i].FileName);
                file[i].SaveAs(HttpContext.Server.MapPath("~/Fotos/")
                                                  + fileName);
                _lista.Add(fileName);
            }
            veiculo.Imagem1 = _lista[0];
            veiculo.Imagem2 = _lista[1];
            veiculo.Imagem3 = _lista[2];
            veiculo.Imagem4 = _lista[3];
            veiculo.Imagem5 = _lista[4];
            db.Veiculo.Add(veiculo);
            db.SaveChanges();
            return RedirectToAction("Index");

    }

What I need is for the user to be able to upload as many images as he wants, not just 5 as he is done in my code.

    
asked by anonymous 05.09.2017 / 18:10

1 answer

2

The ideal in this case, according to normalization of data, is to separate this information into another table in a 1xN relationship.

For example, create the veiculo_imagens table with the id_veiculo_imagens (primary key), id_veiculo (foreign key), and imagem fields.

With this, 1 vehicle can have n images.

    
05.09.2017 / 18:35