Khizar
Khizar

Reputation: 329

Silverlight 4 Entity framework issue

I have created a sample application in Silverlight with RIA services. I am using entity framework for CRUD operation but it does not work for INSERT Operation with following Exception, "Submit operation failed validation. Please inspect Entity.ValidationErrors for each entity in EntitiesInError for more information." I have not apply any validation but don't know how the error occurs. I have tested that when I create an object of DB entity and assign values to it and then save by calling object.SaveChages(), it works fine. But its default method does not work. Any help is appreciated.

Thanks

Upvotes: 0

Views: 1105

Answers (2)

sebagomez
sebagomez

Reputation: 9609

The SubmitOperation callback has an EntitiesInError property which you can use to iterate thru the entities. That's the way of getting the "real" error.

Here's the method I have to show the user what went wrong...

public static bool WasSubmittedOK(SubmitOperation so, string errorMessageHeader, out string errorMessage)
{
    errorMessage = string.Empty;
    if (!so.HasError)
        return true;

    so.MarkErrorAsHandled();

    errorMessage = "An unknown error has occurred";
    if (so.EntitiesInError.Count() > 0)
    {
        StringBuilder builder = new StringBuilder();
        builder.AppendFormat("{0}\r\n", errorMessageHeader);
        foreach (Entity item in so.EntitiesInError)
        {
#if DEBUG
            builder.AppendFormat("\r\nFor {0}", item.GetType());
#endif
            foreach (ValidationResult error in item.ValidationErrors)
            {
                builder.AppendFormat("\r\n- {0}", error.ErrorMessage);
                Debug.WriteLine(string.Format("Error in {0}:'{1}'", string.Join(",", error.MemberNames.ToArray()), error.ErrorMessage));
            }
        }
        errorMessage = builder.ToString();
    }
    else if (so.Error != null) { errorMessage = so.Error.Message; }

    return false;
}

Upvotes: 3

Julie Lerman
Julie Lerman

Reputation: 4622

Are you able to drill into the validation errors? I actually have an article about this coming in December MSDN Magazine Data Points but I bet you don't want to wait, right? :)

Even if you haven't applied any specific validations, there are things like foreign key contsraints that EF will still check. If you can see what the error is that will be ultimately useful in solving your problem. Debug into the exception. See if there is a DbEntityValidationException available...maybe it's in an innerexceptoin. DbEntityValidationException will have one or more EntityValidationErrors. Each of those contains a list of all of the errors found for one instance. That means expanding the EntityValidationErrors items one at a time and looking at the ValidationError items contained within.

Upvotes: 0

Related Questions