EF6 Entityvalidationerrors error in filled property

Asked

Viewed 425 times

3

I have a similar problem in two ASP.NET applications with Entity Framework.

When I try to update an entity that already has all the required attributes filled I get an exception from EntityValidationErrors, however that property is described in validarion this filled.

The strange thing is that if I put the breakpoint and check the attributes of the object they are all filled and it does not cause the error.

In one of the applications I am circumventing this problem by making the property receive itself or simply accessing the property and throwing its value in any variable:

ComentariosBlog model = _comentariosBlogNegocios.GetById(idComentatio.Value);

if (model == null)
    return HttpNotFound();

model.Ativo = status.Value;
var teste = model.Publicacao; //<----Contorno
idPost = model.PublicacaoId;
_comentariosBlogNegocios.Salvar(model);

I already have a block of Try Catch that handles this error, the problem is that the object is with all the mandatory properties filled and I have to do a "Gambi" so that it does not generate errors like this in the code above! The simple fact of accessing the property and throwing it in any variable already stops giving error, but I need a more correct solution to this problem, as I said this is only a "Gambi"!

Below is the details of the error that is generated:

inserir a descrição da imagem aqui

In this image we can see that the mentioned property is already filled:

inserir a descrição da imagem aqui

Here is the error message inside the Try catch:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • possible duplicate of How to fix Entityvalidationerrors

  • There is already an error handling to know which property the RU says is not filled in. The problem is that this property is already filled and is not a simple problem of handling Entityvalidationerrors.

  • I need you to explain in your question how this error handling is done. There are several types of validation that are done by DefaultModelBinder, not only the field completed or not.

  • Good afternoon @Ciganomorrisonmendez, sorry for my ignorance on the subject but I had already done the validation as recommended in the publication [link]http://answall.com/questions/17644/como-correctr-o-entityvalidationerrors[link]. However when I place the breakpoint and check the properties of the object I am trying to save, the property that is pointed out by Dbentityvalidationexception is a property that has value, but if before Savechanges() i put a test variable receiving the property value the code does not generate exception.

  • What error appears in ve.ErrorMessage, in the solution already mentioned?

  • @Ciganomorrisonmendez I made an issue in the question with the prints of the errors, I do not know if it helps?

  • Please put your Model on the question. Something caught my attention on your screen.

  • @Ciganomorrisonmendez you tell my entity as print I entered now?

Show 3 more comments

1 answer

2


Your Model is wrong. The navigation property:

public virtual PublicacaoBlog Publicacao { get; set; }

Can’t be decorated with [Required] because she will not be saved. What is right is PublicacaoId receive the decoration, because this yes will be saved.

Already this filling out:

var teste = model.Publicacao; //<----Contorno

It attests to how much is wrong with your entire project. It’s easy to see around:

ComentariosBlog model = _comentariosBlogNegocios.GetById(idComentatio.Value);
...
_comentariosBlogNegocios.Salvar(model);

Entity Framework already implements a repository and is NOT required to implement a layer of "services", "business" or anything else, because that’s the role of Controller do. I’ve been writing extensively about this for a long time, so I’ll mention it again:

It is no use to say that "the said MVP is good practice". No, it is not.

  • Applying DDD to MVC is not good practice;
  • Layer Service in MVC project is not good practice;
  • Isolate Controllers and Models in DLL’s different is not good practice;

What you do on the line I marked is make the dynamic proxy turn an object, which correctly fills the foreign key of the object, and therefore saves. To solve, put the [Required] in the right field for him, that is:

[Required]
public int PublicacaoId { get; set; }
  • 1

    Thank you @Ciganomorrisonmendez, thank you very much! This explains several problems I’m having in the other project. I imagined that I was doing everything correctly and I didn’t even imagine that I was making a big mistake in architecture. I would also like to thank you for the links this helping a lot to clarify some other doubts!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.