Filip
Filip

Reputation: 1864

Entity framework validation error after updating database and changing model

I am working on ASP.NET MVC app that uses EF to work with database. I needed to change one string property that had StringLength(1000) attribute and was mapped to nvarchar(1000) column in the database.

I updated the database to use nvarchar(max) due to recent requierements and removed StringLength(1000) attribute from the model class.

The problem is. Every time I want to insert new entity that has this property with lenght greater than 1000 I get validation error saying it can be only 1000 characters. When it is lower, everything works as expected.

The field must be a string or array type with a maximum length of '1000'.

I have no idea where is this error coming from. There are no triggers or stored procedures. I also tried using attribute MaxLength or StringLenght(int.maxInt) but got the error. There is also no additional configuration with Fluent API..

EDIT: Unfortunately it does not look like dumb problem with not rebuilding solution. I tried it twice now (+ cleanup) and the problem persits.

SOLUTION: So for some reason there is another model class for the same entity in the project used internally. It was completely without single attribute to its properties. After adding MaxLength to that single property here it works.

Upvotes: 3

Views: 1920

Answers (1)

Mr. B
Mr. B

Reputation: 2965

Given that you cannot regenerate the database, the solution is to implement a partial class and use Custom Validation for your field. It's not so clean, but it should work.

Note: Their may already be a partial class in this case

Please reference this MSDN Article

Upvotes: 1

Related Questions