luciusism, since you posted this, I hope you won't mind a couple of helpful suggestions on your code. If valueToSet is null, then the 'is' operator will return false when comparing to string (or anything else). Also, once you have determined valueToSet is a string, calling ToString() on it is not needed. So you could make your code a little more efficient. (See my code below).
It occurred to me since I decided to stick with OnValidateEntityBeforeSave(), maybe I should put my code up here with an explanation, in case it's helpful to anybody else in the future. I noticed that PreProcessValueToSet was getting called on everything when fetching from the database. So my final solution was to stick with OnValidateEntityBeforeSave(). I keep it in a partial class in my DAL project, along with other partial classes containing code I used to insert directly into the generated files, or else use custom templates for. Using partial classes is much cleaner, if you have a choice.
Here's the entire file, (with a couple of cosmetic name changes):
using System.ComponentModel;
using SD.LLBLGen.Pro.ORMSupportClasses;
namespace XXXXXX.XXX.DAL.EntityClasses
{
public abstract partial class CommonEntityBase
{
protected override void OnValidateEntityBeforeSave()
{
// Genpro considers empty string to be semantically equivalent to null. In the db,
// query results can be very different depending on whether a column's value is
// NULL or set a zero-length string. To fix this, if a changed field currently has
// an empty string, we "correct" that so that NULL in sent to db. I looked also at
// PreProcessValueToSet(), but it gets called for every entity/field when a XXXX is
// loaded from the db, so this seems the best place to do this.
base.OnValidateEntityBeforeSave();
foreach (IEntityField2 field in Fields)
{
if (field.IsChanged)
{
string s = field.CurrentValue as string;
if (s != null && s.Length == 0)
SetNewFieldValue(field.FieldIndex, null);
}
}
}
}
}
This certainly may not be the final word on the subject!
I do think Frans should consider building this option into the designer somehow.
Jim
P.S. Not sure, but I believe the whole "" vs. NULL issue may be moot in Oracle. I'm using SQL Server.