5
I have the following schema in my Mysql BD:
Models have a list of standard_images and each standard_images has a list of standard_regions. I must use Entity Framework 5.0.0. I have a model with 3 standard_images and each of them has 4 standard_regions. I need to update this template where I will remove standard_region 2 and 3 from standard_image 2.
To update, I’m going to the BD, looking for the Id of the model I want to change, and through a foreach I’m scanning the BD model and updating with the data coming from the graphical interface.
But how to update this model when I need to remove a stdRegion, or a Stdimg, IE, when there is the divergence between the lists that originally exist in BD and the new data that the user wants to change, either adding or removing components from both lists ?
The code to delete a standard_region is this:
public void AlterarModelo(Modelo modAlterado)
{
//Busca no BD o modelo a ser alterado
models modBanco = db.models.Find(modAlterado.id);
//apaga do banco caso não exista na lista passada via interface
foreach(var image in modBanco.standard_images)
{
List<standard_regions> lista = new List<standard_regions>();
foreach (var regiao in image.standard_regions)
{
foreach (var a in modAlterado.lstImagemPadrao)
{
if (a.lstRegiaoInteressePadrao.Count(x => x.id == regiao.id) == 0)
{
var regTemp = db.standard_regions.Find(regiao.id);
lista.Add(regTemp);
}
}
}
foreach (var reg in lista)
{
image.standard_regions.Remove(reg);
}
}
//adiciona caso não esteja no banco
foreach (var imgAlterado in modAlterado.lstImagemPadrao)
{
foreach (var imgBanco in modBanco.standard_images)
{
foreach (var regAlterado in imgAlterado.lstRegiaoInteressePadrao)
{
if (regAlterado.id == 0)
{
var regTemp = db.standard_regions.Find(regAlterado.id);
standard_regions sr = new standard_regions
{
coordinate = regAlterado.coordinate,
description = regAlterado.descricao,
standard_images_id = imgBanco.id
};
imgBanco.standard_regions.Add(sr);
}
}
}
}
modBanco.date = modAlterado.data;
db.SaveChanges();
}
The Model, Image and Region classes define the transfer objects where I bring the changed user data in the graphical interface:
public class Modelo
{
public int id { get; set; }
public string nomeModelo { get; set; }
public string statusModelo { get; set; }
public DateTime data { get; set; }
public Usuario usuario { get; set; }
public bool foiInspecionado { get; set; }
public ImagemPadraoColecao lstImagemPadrao { get; set; }
public Modelo()
{
lstImagemPadrao = new ImagemPadraoColecao();
usuario = new Usuario();
}
}
public class ImagemPadrao
{
public int id { get; set; }
public string nomeImagemPadrao { get; set; }
public string caminhoImagemPadrao { get; set; }
public Modelo modelo { get; set; }
public RegiaoInteressePadraoColecao lstRegiaoInteressePadrao { get; set; }
public ImagemPadrao()
{
lstRegiaoInteressePadrao = new RegiaoInteressePadraoColecao();
}
}
public class RegiaoInteressePadrao
{
public int id { get; set; }
public string descricao { get; set; }
public string coordinate { get; set; }
public int imagemPadraoId { get; set; }
}
To improve understanding, it would be an update with an deletion (or addition) of some item from one of the two lists.
– Emerson
It has so many forms and scenarios, because, everything is in relation to the graphical interface that manipulates the data and depending can be direct exclusions since each model (entity) has its
primary key
.– user6026
His model assembly is very peculiar. I still don’t understand where are the statements of
standard_images
andstandard_regions
.– Leonel Sanches da Silva