This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, I implemented a FieldConvertor, however it never seens to get called when the database value is DbNull. This poses a problem if the CLR type on the entity is non-nullable. What I'd like to do instead is to support some default value if the database value is null. Is this possible? Thanks, Werner |
|
|
I've not been able to reproduce this problem. I tested loading from a nullable database column into a int field using the following converter: public class NIConverter : IFieldConverter The "DBNull" branch was taken and I saw the default value appearing in the materialised entities. Could you provide us with a minimal schema and NUnit test or console application that exhibits the problem? Thanks! |
|
|
Here is my code snippet. public sealed class NotificationChannelConvertor : Mindscape.LightSpeed.FieldConverters.FieldConverter<int, NotificationChannel> { protected override NotificationChannel ConvertFromDatabase(int databaseValue) { return (NotificationChannel)databaseValue; } protected override int ConvertToDatabase(NotificationChannel value) { return (int)value; } } Werner
|
|
|
Your field converter is using the strong-typed FieldConverter<TDatabase, TModel> with a TDatabase of int, but your database contains non-int values. This is causing casting errors; there is no way we can pass a DBNull.Value or even a null into your ConvertFromDatabase method because the argument type does not permit it. Change your class to inherit either from FieldConverter<object, NotificationChannel> or directly from IFieldConverter. You will then be able to handle the DBNull case to return a suitable value (see my post above for an example). |
|
|
Thanks Ivan, I'll use IFieldConvertor instead. It would be useful to have a virtual method I can override in the strongly-typed FieldConvertor. By default FieldConvertor can return the default value for TModel. The strongly-typed FieldConvertor is simpler to implement and requires less code.
|
|