0

My problem is I have an existing table that I can not modify that contains fields that are nullable.

I have a model that I do not want nullable properties.

What I want to do is have a convention (I presume this is how) where I can set the default value to use based on the models property type when DBNull is encountered from the database.

  • ints/double = 0
  • string = ""
  • bool = false

Pretty sure this was simple to do in NHibernate but I can't find out how to do this in EF.

I am using the latest EF package from nuget which I believe is EF 4.2.

Galen
  • 309
  • 1
  • 3
  • 15
  • Why wouldn't you want the DBMS to manage this for you? It seems like something that you'd want centrally managed. – M.Babcock Jan 30 '12 at 02:58
  • Legacy system with a metric crap load of fields. Altering the table schema runs the risk of impacting something somewhere that I'm not within scope to handle. Making my model nullable to match the DB is a last resort option. I'm confident it should be possible to introduce a checking convention somewhere as EF binds from the database to the model. – Galen Jan 30 '12 at 03:57
  • I wonder why you do not want nullable properties. Nullable is designed for this scenario. –  Jan 30 '12 at 04:00
  • @allentranks - I didn't think `Nullable` could handle DBNull natively, are you sure it applies? – M.Babcock Jan 30 '12 at 04:12

2 Answers2

1

It is not possible because EF doesn't support custom conventions (unless you hack them in). Moreover even with conventions it would probably not work.

What you are looking for is custom simple type mapping (or simple type conversion). It is very important feature of ORM but till now it is completely ignored in EF. Currently DB type should match your type in EF model otherwise you can have serious problems because you cannot do any conversion inside mapping. The first conversion will be supported in EF 5 and it will only support converting int to enum (hardcoded conversion).

In EF nullable type in database => nullable type in your model.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
0

As alentranks hints at above, this could possibly be handled using some magic with Nullable<T> properties. In my experience Nullable<T> doesn't handle DBNull on its own (though I could be wrong), you could use a trick defined here:

private static T NullValue<T>( object testValue, T nullValue )
{
    T returnValue;
    if( testValue is DBNull )
    {
        returnValue = nullValue;
    }
    else if( typeof(T).GetGenericTypeDefinition().Equals( typeof(Nullable<>) ) )
    {
        returnValue = (T)Convert.ChangeType( testValue, Nullable.GetUnderlyingType( typeof(T) ) );
    }
    else
    {
        returnValue = (T)Convert.ChangeType( testValue, typeof(T) );
    }

    return returnValue;
}
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • Thanks for this but it's not quite what I'm after. I know my model can be made to handle Null types. The option I'm trying to explore is automatic conversion of these DBNulls via convention. In NHibernate I believe you implement an IPropertyConvention and can do the checking in there. Im chasing an EF equivalent. – Galen Jan 30 '12 at 05:00
  • Perhaps [the answer to this question](http://stackoverflow.com/questions/3137738/ef4-code-only-how-to-set-default-value-for-pocos) would be more appropriate then. – M.Babcock Jan 30 '12 at 05:12