ModelValidationException was unhandled user code

Can you help me on the following error, i checked everything no ID mistakes

ModelValidationException was un handled by user code

An exception of type ‘System.Data.Entity.ModelConfiguration.ModelValidationException’ occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:

 public int GetCount()
    {
        ShoppingCartId = GetCartId();

        // Get the count of each item in the cart and sum them up          
        int? count = (from cartItems in _db.ShoppingCartItems
                        where cartItems.CartId == ShoppingCartId
                        select (int?)cartItems.Quantity).Sum();
        // Return 0 if all entries are null         
        return count ?? 0;
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Frustratingly .net does not always show you the inner exception. Wrap your code in a try block with this catch

catch (DbEntityValidationException ex) {
    // Retrieve the error messages as a list of strings.
    var errorMessages = ex.EntityValidationErrors
            .SelectMany(x => x.ValidationErrors)
            .Select(x => x.ErrorMessage);

    // Join the list to a single string.
    var fullErrorMessage = string.Join("; ", errorMessages);

    // Combine the original exception message with the new one.
    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

    // Throw a new DbEntityValidationException with the improved exception message.
    throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x