Print Page | Close Window

ShouldTreatEmptyStringAsNull issue

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2518
Printed Date: 13-May-2026 at 7:18pm


Topic: ShouldTreatEmptyStringAsNull issue
Posted By: hijaggu
Subject: ShouldTreatEmptyStringAsNull issue
Date Posted: 18-Feb-2011 at 6:43pm

Hi,

I believe we have found an invalid EntityManagerSaveException.

We have a Mssql data field (string) that is set to not allow NULL.  However, an empty string is acceptable.

We have changed the flag ShouldTreatEmptyStringAsNull to false in order to allow an empty value not cause a verification system exception.  This seems to work properly.  However, the entity still has a property IsRequired=true in the autogenerated class and this causes an EntityManagerSaveException to be thrown when saving the object (xyz property is required).    This seems to be conflicting behavior. Would you please suggest how to handle this situation ? 


- Jignesh Suthar 




Replies:
Posted By: sbelini
Date Posted: 22-Feb-2011 at 1:03am
Hi Jignesh,
 
You could use a "buddy" class and create a verifier to override the verifier in the generated code.
 
The "buddy" class is a new feature available in DevForce 6.0.8.
 
You will find more information about in our http://drc.ideablade.com/xwiki/bin/view/Documentation/Adding-Validation-Attributes-to-Generated-Properties - DevForce resource Center .
 
Silvio.


Posted By: hijaggu
Date Posted: 22-Feb-2011 at 9:19am
Thanks for your reply. If I understand  correctly, Buddy class means Metadata class?? If thats true, I already tried it. Challenge with buddy class is, I  need to create buddy class for each entity and for each string property I need to specify verifier which would be very cumbersome.  Is there a workaround which I can implement as global setting for each entity?


- Jignesh  


Posted By: sbelini
Date Posted: 22-Feb-2011 at 5:41pm
Hi Jignesh,
 
Sorry for the confusion.
After some investigation and better understanding of your problem, I found out that the actual issue is that even though you set ShouldTreatEmptyStringAsNull = False in the client, you haven't done so in the Server.
 
This is necessary because in your particular case you are setting the VerifierEngine "less strict" than it was originally. So, you most also "loose it up" in the server.
 
You should do that in a SaveInterceptor, in the ValidateSave override: (have this in the very beginning)
 
EntityManager.VerifierEngine.DefaultVerifierOptions.ShouldTreatEmptyStringAsNull = false; 
 
In our DevForce Resource Center, you will find more information about http://drc.ideablade.com/xwiki/bin/view/Documentation/Server-side-validation - Server-Side Validation and http://drc.ideablade.com/xwiki/bin/view/Documentation/Server-Side-Life-Cycle-Events - Server-Side Life Cycle .
 
Regards,
   Silvio.


Posted By: scottarlp
Date Posted: 06-Jul-2011 at 9:29am
I'm a little confused as to what and where exactly you perform these changes. I've read the following link and the surrounding ones several times and I can't tell if I need to add a class to my EF project or what. I simply want to turn off treating empty strings as nulls since they aren't to begin with.
Can you tell me exactly what and where I put code or set properties to change this?
 
Thanks.
 
http://drc.ideablade.com/xwiki/bin/view/Documentation/validation-configure - http://drc.ideablade.com/xwiki/bin/view/Documentation/validation-configure
 


Posted By: sbelini
Date Posted: 06-Jul-2011 at 9:50am
Hi scottarlp,
 
By SaveInterceptor I mean a sub-class (server side) of EntityServerSaveInterceptor where you can override its virtual properties and methods. (i.e. ValidateSave method)
 
You can find more information about save interceptors in the http://drc.ideablade.com/xwiki/bin/view/Documentation/save-lifecycle-server - DRC .
 
In the client you can simply use the one-liner snippet provided in my previous post. (be sure to replace 'EntityManager' with your EM instance)
 
Regards,
   Silvio.


Posted By: scottarlp
Date Posted: 06-Jul-2011 at 10:15am
Thank you. That gave me enough to figure it out. It would help if IB would have some concrete examples of some of this. There's a lot of references to all the properties and overrides, but I couldn't figure out this simple code on piecing it all together. Just in case someone else needs to see the actual code to do this, I'm attaching below. I added a new class to my EDM project and the below code.
 
using IdeaBlade.EntityModel.Server;
public class EntityServerSaveManager
: EntityServerSaveInterceptor
{
protected override bool ValidateSave()
{
EntityManager.VerifierEngine.DefaultVerifierOptions.ShouldTreatEmptyStringAsNull = false;
return base.ValidateSave();
}
}


Posted By: scottarlp
Date Posted: 18-Oct-2011 at 7:39am
I have another issue with the empty string problem with DevForce. I have previously used the above code to take care of the server side and it has worked great. Recently, I started using Verifiers on the client side to provide more custom logic. I also moved to WPF and started using the ValidatesOnDataErrors, which was great.
 
However, the empty string issue has surfaced now on the client side. How do I either remove or change the client side verifier to allow empty string in a non-nullable field (which by my history of programming is valid)? I've tried adding a RequiredValueVerifier, setting the treatasnull to false and the string length one allowing minimum of zero and treatasnull to false (couldn't figure out how to set max length, so I set it to null). Neither of these appeared to change anything.
 
So I think my question is how do I remove (or alter) the default required length > 0 verifier from non nullable string fields during client side validation?
 
Thanks for any help.
Scott


Posted By: sbelini
Date Posted: 21-Oct-2011 at 11:37am
Hi Scott,

The same approach used server-side should work. The only difference is that you should refer to your Entity Manager instance instead:

myEM.VerifierEngine.DefaultVerifierOptions.ShouldTreatEmptyStringAsNull = false;

Regards,
Silvio.


Posted By: scottarlp
Date Posted: 24-Oct-2011 at 5:16am
Such a simple solution - I didn't look an an instance of the manager. That did exactly what I needed.
 
Thanks!
Scott



Print Page | Close Window