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.
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.