Entity framework error when inserting model - asp.net-mvc

Having problem with inserting a model to database with EF in a MVC application.
Im receiving following error when callning dbContext.SaveChanges() :
[DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.]
Any clue what this can be about?
Its strange because sometimes it works to make one insert but if i make another immediately after it crashes.
And sometimes it crashes on first insert.

You can check the accepted answer in this post: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
PS: Please remove error message details as it's of no use and it can expose private information.

More than likely you have some property on the entity that is not nullable. Most of the time when I get this error, that's the culprit. Set a watch on the 'EntityValidationErrors' property, then in the debugger expand into this property and you will see the exact message that is being thrown.

Related

Failed to set the ‘_$visited’ property on ‘DOMStringMap’: ‘data-_$visited’ is not a valid attribute name

We are using breeze.js with entity framework to initiate client side entity management.
We randomly get "Failed to set the ‘$visited’ property on ‘DOMStringMap’: ‘data-$visited’ is not a valid attribute name" error the the breeze.js from __toJSONSafe method of it.
does anybody have any idea what could make "obj._$visited" property undefined? It is coming up as undefined and that is causing the issue during call to the saveChanges()
I'm guessing ... it seems like you've added some kind of DOM object to the entity before saving it. I can't imagine how else you could be subjecting the DOMStringMap to __toJSONSafe.
Would need to know more precisely what object (and entity) is involved when you get this exception.
You say it happens randomly. That doesn't make it easy on any of us. If you can make it happen often enough to detect, you could patch the __toJSONSafe method in your local copy of breeze.debug.js so that you can better trap the error and the information about what makes it happen.
Come back and share that information with us.

Unacceptable type of value for to-one relationship: property = "list"; desired type = AbstractList; given type = List;

I have been struggling with this stupid error for a full day now! As the error shows, it rejects the assignment because the "given type" is different than the desired type. This is wrong since AbstractList is a parent entity for List! I tried the following:
Re-generation of all core data entities classes from the model.
Casting to AbstractList (which sounds silly but the error is stupid and vague in the first place!).
How can I fix this?
As it turns out, I made a mistake when creating the core data stack. I am using a singleton to manage core data with an interface that supports CRUD operations. The bug was simply creating a new stack everytime I perform an operation! Yes, it's a stupid mistake same as the stupid error. I better go now and write my tests to make sure this class is robust enough.

Breeze getValidationErrors not working?

A Breeze EntityMananger is throwing an exception when I try to save two related entities. The exception is being thrown client-side before any communication happens with the server. The error message in the exception is "Validation error", but calling EntityAspect.getValidationError() on each entity listed in the exception returns an empty array.
The screenshot below shows a live example:
Question: Is there some other way of finding out what exactly the validation problem is or is this a bug?
Thanks for your time!
I found the cause of the behavior. rejectChanges() is called on the EntityManager for the entities in question before the exception raised by saveChanges() is rethrown, to be handled by the code in the example above. The error state of the entities is not preserved in the exception object, so calling getValidationErrors() on each entity after calling rejectChanges() returns an empty array since at that point the changes have been rolled back and there technically aren't any errors on the entity.
So, technically not a bug. Breeze is working as designed. However, I think there's an argument to be made that the exception object passed when saveChanges() fails should contain a complete and immutable description of the problem(s) that caused the exception.

There is no ViewData item with the key 'Blah' of type 'IEnumerable<SelectListItem>'

This error message is driving me nuts.
I'm getting it when using Html.ListBox and Html.DropDownList HtmlHelpers with ASP.NET MVC v1.0. Populating the lists works OK - I can view them, etc - but when I go to create a new record in the Model using the FormCollection passed into the Controller to get the selected value, this error occurs. I've tried several different configurations and code arrangements as per several other posts on this matter - no joy.
Hopefully someone can tell me whether this is a known issue or not so as I can move on with my life! Would prefer not to have to post code if necessary, but can do if requested.
Oh, and a suggested workaround/solution would be nice too ;-)
TIA!
Bernard.
Ok, quick update, I've posted three comments to responses below clearing up details as to what I'm up to here.
I now have another issue to share - I've tried ScottGu's alternative recommended approach - implementing a ModelView. So, when I do this it's basically the same scenario, but I get an "Object reference not set to an instance of an object." error at the same place that I was getting "There is no ViewData item with the key 'Blah' of type 'IEnumerable'.".
I'm starting to thing that this is perhaps something to do with the way the EF works...
Ok, sooo I'm clearly an EF/MVC noob...
Although the issue was solved by my actually implementing proper exception management...the exception message (when I actually started catching it) was "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.". So I'm using the same ObjectContext for all data access now, and things are back on track.
Thanks for caring, I'll have a smarter question next time - promise!
Bernard.
Not knowing how you are coding this will be hard for people to identify the problematic area. Have a read of this forum thread - further down has a similar error to you.
http://forums.asp.net/p/1320426/2623986.aspx
You can again bind your select list in the according post method where validation occurs.
For example you have an error inside the create view of the controller than you can again bind dropdown list in create post method.

Custom validation summary

I'm using the UpdateModel method for validation. How do I specify the text for the error messages as they appear in the validation summary?
Sorry, I wasn't entirely clear. When I call UpdateModel(), if there is parsing error, for example if a string value is specified for a double field, a "SomeProperty is invalid" error message is automatically added to the ModelState.
How do I specify the text for said automatically generated error message?
If I implement IDataErrorInfo as suggested, it's error message property gets called for every column, regardless of whether the default binder deems it valid or not.
I'd have to reimplement the parse error catching functionality that I get for free with the default binder.
Incidentally, the default "SomeProperty is invalid" error messages seem to have mysteriously dissappeared in the RC. A validation summary appears and the relevant fields are highlighted but the text is missing! Any idea why this is?
Thanks again and I hope all this waffle makes sense!
This tutorial is a good example of the IDataErrorInfo technique - it makes adding validation parameters easy by adding them as attributes directly to the properties of the model classes.
These examples also may help - slightly different approaches to validating.
Additionally, this creative idea (which also implements IDataErrorInfo) may be a help to you.
Implement IDataErrorInfo on your model.

Resources