Error saving fields with special characters using EntityFramework and MVC ModelBinding

I am having problems with (2) fields when saving objects in an ASP.NET MVC4 application:

  • Email
  • telephone

As you know, these fields will have special characters @ and - as a rule. Here is my controller code using ValueInjecter to input model classes from a hydrated object through model binding:

 public ActionResult Create(TestViewModel testViewModel) { using (var context = new MyEntities()) { var person = new Person(); person.InjectFrom(testViewModel); context.Person.AddObject(person); context.SaveChanges(); } } 

The error I am getting is the following:

"String or binary data will be truncated. Terminated"

Well, at first glance you can say that this is the size of the field in SQL, but the true problem is masked due to these special characters. If I enter the following for the fields:

tel .: 11234565454514564561
email: blahblahblahblah

... then everything works .

However, if I enter:

tel .: 123-456-7890
Email: test@wow.com

I get the error "String or binary data will be truncated." So I found a job, but it seems completely unnecessary and probably wrong. If I explicitly map the values ​​from the associated model object to the entity using C # escape characters, then my values ​​will be inserted as shown below:

 public ActionResult Create(TestViewModel testViewModel) { using (var context = new MyEntities()) { var person = new Person(); person.InjectFrom(testViewModel); //Explicitly map items preserving special charachters person.EmailAddress = @testViewModel.EmailAddress; person.Phone = @testViewModel.Phone; context.Person.AddObject(person); context.SaveChanges(); } } 

Well, honestly, I don’t know where the criminal is. Is this a MVC model binding problem, an Entity Framework problem, a C # deal, or a ValueInjecter? I tried using annotations for my ViewModel properties to dictate data types such as Phone and Email , but this did not stop the problem. This was done only by being explicit in the code, eluding those characters to do so.

What am I doing wrong because I know that fields like email and phone with special characters are stored all the time in EF using MVC? What can I do to avoid having to sprinkle these lines of code every time I have a value with a special character?

Thanks!!

+4
source share
1 answer

Well in a typical fasion, I figured it out 10 seconds after posting the question, which I spent 3 hours trying to figure out:

As @CodeMaster noted, these lines of code were wrong anyway. I started playing with some other fields, and I understood the criminal.

The code I gave was compressed for brevity, but I introduced various concepts in (2): Person and Contact. It turns out that both tables have a "Title" field. I focused only on the "Title" field in the "Personality" table, the length of which was "50", as shown below:

<Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="50" />

It turns out that the Contact field has the exact smae field, which I did not know about, but only the length "8", as shown below:

<Property Name="Title" Type="nvarchar" MaxLength="8" />

This caused a mistake, and it was really legal. ValueInjecter matched the same same β€œName” value for both objects when I just wanted it and caused a problem. You need to be careful, because ValueInjectoer does not distinguish if the names match (as it shouldn't), but it causes my problem.

+2
source

All Articles