August 20, 2014

Form Data is not valid

I have this asp.net web forms application through which users can log on and view a number of records from a database and then make modification as needed. However, as the number of records have increased for users; for some users with a large number of records, the site is failing with the message "form data is not valid".

Since the issue is happening only for large accounts, I was somewhat certain that it's related to the number of form elements that can be posted back to the server. Microsoft introduced an upper limit to the number of the form elements that can be posted back to the server - http://weblogs.asp.net/scottgu/archive/2011/12/28/asp-net-security-update-shipping-thursday-dec-29th.aspx. So, to fix the issue, I added a new app setting in web.config file called "aspnet:MaxHttpCollectionKeys" and set its value to a number - 5000. And that's it - the site started working fine again.

Just to be clear though, I think this is a temporary fix and the application should be modified anyway as sooner or later that new limit might be reached as well.

I build an application using Visual Studio and Sql Server and built the model using Entity Framework but when I deployed the application, I keep getting an error. To give bit more information, I set up the database using Sql Server 2012 and applied the compatibility level to Sql Server 2005 as the database server only had Sql Server 2005. Ya, I know it's an old server but it's not a priority to the business to update that server.

So, after running the application against the production database, I realised that the error is happening from the database server and not from the web server. Basically, it's failing to a add a record when the table has a datetime column and it's failing with the message "The version of SQL Server in use does not support datatype 'datetime2'". I was a bit confused as I thought I have applied the compatibility settings correctly.

To further test, I reloaded the data model from the Sql Server 2005 database but the error was still there.

Now, to fix the issue, I opened the edmx file in an xml editor and within <edmx:StorageModels> element there is "ProviderManifestToken" attribute in "Schema" element. The value for "ProviderManifestToken" was set to 2008. Bingo. I changed that value to 2005 and recompiled the application which fixed the issue.

This is probably a bug in Entity Framework as it not picking up the right database and instead applying a default lowest value.

To apply the compatibility level, right click the database name in SQL Server Management Studio, and click on Properties, navigate to Options and then set compatibility level.

image

Models in an MVC application are just classes and therefore it is easy to add data annotations in the class itself. When MVC generates the views, the different field names and validation messages and datatypes are automatically generated. For example, the following can be a data model in an application.

The above LoginModel is copied from the auto generated template in an MVC application. Note that the data annotations like "Required", "DataType" are applied. However, if the model is generated from Entity Framework, these data annotations will not appear and how would we add the necessary data annotations.

So, to add annotations to an Entity model, we can take advantage of the System.ComponentModel.DataAnnotations.MetadataTypeAttribute class that allows to specify the metadata class to use with the data model class. So, we need to create a new class specify the metadata attribute which will link the data model class to another class which will contain all the data annotations for the necessary fields.

Here is an example of how this would work.

[MetadataType(typeof(AccountMetaData))]
public partial class Account
{
    //empty class just to apply class level attribute
    //imagine this as data annotations for Account
    //is in AccountMetaData class
}

public class AccountMetaData
{
    //same property name & type as in the Entity model
    [Display(Name = "Date of Birth of date")]
    public DateTime DateOfBirth;

    [Required]
    [MinLength(6, ErrorMessage = "Too short")]
    public string AnnountName;
}

I am working on an older product and updating it to use the latest version of .NET and Entity Framework. Now that I have created the Entity Framework model using database first approach, I am finding issues like "EntityType 'EntityName' has no key defined". Basically, what this means is that the table in the database does not have a primary key defined. To fix the issue, the database needs to be updated and a primary key needs to be defined.

I have downloaded a visual studio solution from a source control repository like github or tfs but now the project is not building. It's giving an error message - referenced component 'EntityFramework' could not be found.

In this scenario, the issue is that Entity Framework has been added as a NuGet package and the package has not been restored. It's not necessary to store packages in source control as these can be downloaded and installed on the fly. So, to enable automatic restore of packages, click on Tools > NuGet Package Manager > Package Manager Settings . A prompt will appear where "Allow NuGet to download missing packages" and "Automatically check for missing packages during build in visual studio" needs to be ticked.

Reference: Shahed Kazi at AspNetify.com