NeCroFire

NeCroFire

  • NA
  • 86
  • 0

Entity Framework with conditional default values

Nov 8 2010 3:20 AM
Hi

We are having some problems with default values in EF.
In every table we have these 4 columns:
DateCreated
DateUpdated
UserCreated
UserUpdated


When you insert a new record all 4 fields need to get a value, but when you update a record only the 2 update fields needs new values.

We can set these fields in code every time, but we would like it to be more automatic. The biggest obstacle is when you add a child object to a parent object.
Example:
ContactNumber _Number = new ContactNumber();
_Number.Number = 0826224710;
_Number.Type = "Home";
Client.ContactNumber.Add(_Number);

If you don't specify any values for the date fields, you get a date out of range error from SQL.

What we would like to do is set the User Details at login and have the entity model set the values to those 4 fields when ever a record is either added or updated without us having to set it "by hand" every time. This code should also be un-effected when updating the model from the DB.

I hope this makes sens and that someone can help.

Thanks in advance :)