Null object pattern with entity framework code first

759 Views Asked by At

We've implemented the null object pattern in our domain model. We're using code first with fluent api to persist our domain model.

One of our entities has a navigation property - this navigation property is a FK and is where we use the null object pattern.

Our null object has the default 0 value for it's id. Is there a way with fluent api (or direct mappings) to tell EF 'if this is one of those null objects, REALLY throw null in the database fk field, and not that invalid 0 id'.

EDIT:

As an example, imagine you have a Student Entity, and a FavoriteSubject entity. A student has a reference to a FavoriteSubject, unless he has no FavoriteSubject, then the null object would be something like NoFavorite.

When we persist this to the database, we get the Id of NoFavorite (0) where we'd really like NULL in the db as NoFavorite is a null object (follows null object pattern).

0

There are 0 best solutions below