0

Its properly because I do not understand completely, but I have had these issues a few days now.

I have a datetime field and using entity framework.

Scenarios:

  1. When I set the field to be computed (databasegenerated option) I cant update the field like this:

        db.ComputeUnites.Find(this._ComputeGuid).HeartBeat = DateTime.UtcNow;
        db.SaveChanges();
    
  2. When I don't set it to computed, and set it to required. I get an error when I try to add a new element that do not have the field set. (this makes sense, but I have set the default value on the server to set it to (getutcdate())- so I hoped it would kick in when not set.

  3. I tried to set it to optional and with a default value again, then I can add new objects, but the default value is not used, instead 0:0:0 00:000 (don't remember the syntax correctly) times are inserted.

What I would like is: When I do not set the field on a newly added object, the default value should be used. And if I provide or update the field, it should use the value coming from my c# class.

What am I missing?

1 Answer 1

1

What I would like is: When I do not set the field on a newly added object, the default value should be used. And if I provide or update the field, it should use the value coming from my c# class.

That is not supported. You can either have it store generated and EF will use the value set in database but never use the value set in the application or you can have it application generated and EF will never use the default value from the database because it always have default value in your application which is send to the server.

The simplest workaround is simply set correct default value in the application directly and don't use database default values and store generate pattern:

public class ComputeUnit {
    ...

    public ComputeUnit() {
        HeartBeat = DateTime.UtcNow;
    }

    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.