Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Any setting to convert null strings to empty automatical
Message
From
30/09/2014 01:58:44
 
General information
Forum:
ASP.NET
Category:
Entity Framework
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01608410
Message ID:
01608511
Views:
32
Isn't that MVC code? I'm not sure of my ground here but that should give empty strings in the EF model - but EF will still try to convert the empty string to null when pushing to the DB unless told otherwise.


>Looks like your original suggestion was right on the money. While I was looking at that code and writing a new issue https://efreversepoco.codeplex.com/workitem/73
>my colleague went ahead and made a change in the global.asax file to make sure this property is off (e.g. the empty strings are not converted to null).
>
>
>public class EmptyStringModelBinder : DefaultModelBinder
>    {
>        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
>        {
>            bindingContext.ModelMetadata.ConvertEmptyStringToNull = false;
>            Binders = new ModelBinderDictionary() { DefaultBinder = this };
>            return base.BindModel(controllerContext, bindingContext);
>        }
>    }
>
>    public class MvcApplication : System.Web.HttpApplication
>    {
>        protected void Application_Start()
>        {
>            ModelBinders.Binders.DefaultBinder = new EmptyStringModelBinder();
>...
>
>
>I assume that change was enough to make the problem go away. I haven't yet tested the changes as I didn't yet get them. I assume they fixed the problem.
>
>I would not figure out such changes myself.
>
>
>>>>Did I misunderstand your explanation. I thought you said:
>>>>(a) SQL database does not allow Null strings ?
>>>>(b) You were having a problem trying to save an empty C# string.
>>>>If so then ConvertEmptyStringToNull(false) sounds like what you need.
>>>
>>>Please read my original question carefully again.
>>>
>>>I'll repeat the situation.
>>>
>>>1. In our database we have columns which do not allow null values (all data types except dates are not-nullable in our database).
>>>
>>>2. We're using Reverse POCO generator to generate our data and models and then custom scaffolding templates to generate view models, adapters, repository, angular forms, etc.
>>>
>>>3. When we add a new row in our form, the string values are NULL unless we put values into them.
>>>
>>>4. We need to save these NULL values as empty strings.
>>>
>>>Our original solution was similar to discussed in that thread because the original code was saving strings in upper case. So, there was explicit code in the SaveChanges method that saved null strings as empty.
>>>
>>>We later decided there is no need for converting all strings to upper case. So, I commented out the call to the conversion. As a result of this that new problem of saving NULL strings as empty strings arrived.
>>>
>>>We can go back to the solutions suggested in the thread I mentioned by using some code in the SaveChanges method. I think the code in that thread is more efficient than our original.
>>>
>>>Or, alternatively, we may try to implement getter for the property (it is actually the solution I implemented in a few ViewModels manually).
>>>
>>>I think getter property sounds like a better idea. I already found the code where we may want to make this plug:
>>>
>>>
>>>Entity = string.Format("public {0}{1} {2} {3}{4}", PropertyType, CheckNullable(this), PropertyNameHumanCase, IsStoreGenerated ? "{ get; internal set; }" : "{ get; set; }", comments);
>>>
>>>I am also thinking I'll write a request to the author of the Reverse POCO generator to add some extra property and include this getter into new version of his generator.
>>
>>Sounds like the first suggestion of modifying the code generator might work:
<#+ if (primitiveProperty.TypeUsage.ToString().Split('.').Last() == "String") { #>
>>   if (value == null) value = "   ";
>>   else value = value.Trim();
>><#+ } #>
If value isn't an empty string then it should get back to the DB OK. If you must have an empty string in the DB then set value to an empty string and use in conjunction with the ConvertEmpyStringsToNull(false).
>>
>>Haven't tried it but it sounds viable.....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform