This is my upgrade method:
public dynamic Atualizar(TEntity obj)
{
dynamic data = null;
using (ClassContexto ctx = new ClassContexto(ClassMaster.conexao()))
{
try
{
ctx.Entry(obj).State = EntityState.Modified;
ctx.SaveChanges();
data = "200";
}
catch (DbEntityValidationException e)
{
data = e.EntityValidationErrors;
}
catch (Exception erro)
{
try
{
data = erro.InnerException.ToString();
return data;
}
catch
{
data = erro.Message.ToString();
return data;
}
}
finally
{
}
}
return data;
}
I have a Poi class, which has a collection of contacts. To save, I pass an object of type Poi with a collection of contacts. It works right.
The problem is to do update, I pass the same Poi object (with a collection of contacts) to the above method, but it only changes the Poi table. Is there something wrong with the method?
This is the test method. Here's how I populate objects:
[TestMethod]
public void updatePoiContato2()
{
poiModel.t0031_id_poi = 56;
poiModel.t0030_id_tipo_poi = 2;
// poiModel.t0030_id_tipo_poi = poiModel.t0030_id_tipo_poi;
poiModel.t0031_razao = "TESTE UPDATE" + DateTime.Now.ToString();
poiModel.t0031_fantasia = "TESTE UPDATE" + DateTime.Now.ToString();
poiModel.t0031_cnpj_cpf = "12.935.236/0001-97";
poiModel.t0031_ie_rg = "234234324234";
poiModel.t0031_situacao = 1;
poiModel.t0031_dt_cadastro = poiModel.t0031_dt_cadastro;
poiModel.t0031_alldata = poiModel.t0031_razao + poiModel.t0031_fantasia + poiModel.t0031_cnpj_cpf + poiModel.t0031_ie_rg;
PoiContatoModel model1 = new PoiContatoModel();
model1.t0031_id_poi = 56;
model1.t0033_id_contato = 6;
model1.t0033_nome_contato = "TESTE UPDATE";
model1.t0033_tipo_enum = "CELULAR";
model1.t0033_valor_enum = "41-9174-3185";
PoiContatoModel model2 = new PoiContatoModel();
model2.t0031_id_poi = 56;
model2.t0033_id_contato = 7;
model2.t0033_nome_contato = "TESTE UPDATE";
model2.t0033_tipo_enum = "CELULAR";
model2.t0033_valor_enum = "41-9174-3185";
ICollection<PoiContatoModel> contatos = new List<PoiContatoModel>();
contatos.Add(model1);
contatos.Add(model2);
poiModel.Contatos = contatos;
var result = poiDto.save(poiModel, "100", "a");
Assert.AreEqual(true, result.Contains("200"));
}