When I use the update service, this error occurs:
The 'DataLib' property on 'Release' could not be set to 'System.Double' value. You must set this property to a non-null value of type 'System.Single'
This error happens when
[Route("{id}/{value}")]
public void AtualizaLiberacao(int id, string value)
{
var lista = contexto.Liberacoes
.Where(l => l.IdOrcamento == id)
.ToList();
lista.ForEach(f =>
{
f.FlagLiberacao = 0;
f.AutorizouReceberAtrazado = value;
});
contexto.SaveChanges();
}
I've already changed the Model field to: double, float, single, and decimal , and still the problem continues. As if Entity or Lambda did not recognize the type. I know a lambda expression does fetch all of the records. See, I just want to update two fields, a string and another byte. Can you make a lambda to bring only the fields in question? I tried to give a select and it did not work, like:
var lista = contexto.Liberacoes
.Where(l => l.IdOrcamento == id)
//.ToList()
.Select(s => new Liberacao
{
FlagLiberacao = s.FlagLiberacao,
AutorizouReceberAtrazado = s.AutorizouReceberAtrazado
}).ToList();
When I do the above, it gives this error:
The entity or complex type 'Inet.AuthorizerService.Infra.Data.Context.Liberation' can not be constructed in a LINQ to Entities query.
As I said, I've tried to change the type and it still does not work. I have no more resources.
EDIT1
In the get service I passed in the DTO everything to string and I was able to resolve this error. Well, I tried to do it the same way, as below:
[Route("{id}/{value}")]
public void AtualizaLiberacao(int id, string value)
{
var lista = contexto.Liberacoes
.Where(l => l.IdOrcamento == id)
.Select(libera => new LiberacaoDTO
{
TipoVenda = libera.TipoVenda,
IdOrcamento = libera.IdOrcamento,
Juros = libera.Juros.ToString(),
Entrada = libera.Entrada.ToString(),
Acrescimo = libera.Acrescimo.ToString(),
Desconto = libera.Desconto.ToString(),
Mensagem = libera.Mensagem,
DataLib = libera.DataLib.ToString(),
Vencimento = libera.Vencimento.ToString(),
Vendedor = libera.Vendedor,
Cliente = libera.Cliente,
Filial = libera.Filial
})
.ToList();
lista.ForEach(f =>
{
f.FlagLiberacao = 0;
f.AutorizouReceberAtrazado = "Testando";
});
contexto.SaveChanges();
}
Well, the error has disappeared, but I have two problems with this. You have not updated and the value parameter that I'm passing in URL is coming null . As the list is Liberation and not DTO, I thought it could be done that way. I'm waiting. I passed the AuthorizedReceiveAtrazado value as it can be seen as a literal and still did not update. If anyone can give me that strength, I'll wait.
EDIT2
I've been missing the service.
[RoutePrefix("api/Atualiza")]
public class AtualizaController : ApiController
{
AutorizadorContext contexto = new AutorizadorContext();
PedidoLiberacao liberacao = new PedidoLiberacao();
[Route("{id}/{value}")]
[AcceptVerbs("Put")]
public void putItensLiberacao(int id, string value)
{
liberacao.AtualizaLiberacao(id, value);
}
}
In the string value parameter, there was a [FromBody] , I removed the parameter and is now coming with the correct value but does not update.
EDIT3
I tried to do this:
var lib = new Liberacao();
lib.AutorizouReceberAtrazado = value;
lib.FlagLiberacao = 0;
contexto.Entry(lib).State = EntityState.Modified;
contexto.SaveChanges();
But when I save, it gives this error:
Store update, insert, or delete statement number of rows (0). Entities may have been modified or deleted since groups were loaded. See link for information on understanding and handling optimistic concurrency exceptions.
Now I'm going to do as colleague Gabriel Colleta said
EDIT 4
List<LiberacaoDTO> lista = new List<LiberacaoDTO>();
var lib = lista
.Where(dto => dto.IdOrcamento == id)
.Select(libera => new Liberacao
{
TipoVenda = libera.TipoVenda,
IdOrcamento = libera.IdOrcamento,
Juros = float.Parse(libera.Juros),
Entrada = float.Parse(libera.Entrada),
Acrescimo = float.Parse(libera.Acrescimo),
Desconto = float.Parse(libera.Desconto),
Mensagem = libera.Mensagem,
DataLib = float.Parse(libera.DataLib),
Vencimento = float.Parse(libera.Vencimento),
Vendedor = libera.Vendedor,
Cliente = libera.Cliente,
Filial = libera.Filial,
FlagLiberacao = libera.FlagLiberacao,
AutorizouReceberAtrazado = libera.AutorizouReceberAtrazado
}).ToList();
lib.ForEach(l =>
{
l.AutorizouReceberAtrazado = value;
l.FlagLiberacao = 0;
});
contexto.SaveChanges();