EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

Document dehavior around default value handling, .NET type defaults and sentinel values

Open roji opened this issue 6 months ago • 1 comments

An extremely common issue is users getting confused about EF's behavior around default values: they e.g. set a default true value for a bool property, and are then surprised by not being to insert false.

Our documentation on generated values doesn't explain how this all works, and although there are some bits and pieces in the change tracking section, we're missing a simple, basic section that users would find and understand. EF 8.0 also added the ability to configure the sentinel value, but apart from the what's new section this has not been documented.

roji avatar Jun 23 '25 17:06 roji

Yes, please; also please document what happens when ValueGeneratedOnAdd meets the CLR default values, and once set, how do I set it back to generate it automatically. It is especially confusing to the "value types" as the values "must be set" (or otherwise set to the default) before it can be used, but how do we make it autogenerate? What happens when a long (non-nullable) is set to e.g. 3 but then 0 then it is called with SaveChanges()?

I am still searching for a good answer, the documentation says: "ValueGeneratedOnAdd" is called unless it is a temporary value or null, but we don't know what a temp value is. Is it the CLR default?

What happens when I do, for instance: Definition:

public class Blog
{
    public int BlogId { get; set; } // PK, ValueGeneratedOnAdd
    public string Url { get; set; }
    public int Rating { get; set; }
    public string Owner { get; set; }
}

Code:

var json = @"{
""Url"": ""https://example.com"",
""Rating"": 5,
""Owner"": ""Alice""
}";

var blog = JsonSerializer.Deserialize<Blog>(json);

ctx.Blogs.Add(Blog);
ctx.SaveChanges()

where BlogId is e.g. BIGSERIAL in Postgres (or anything else, where it is ValueGeneratedOnAdd that has to be turned into INSERT) and is not set?

Thank you very much in advance. I'm trying to find out what happens, but I could not locate anything in docs.

ris-work avatar Sep 10 '25 10:09 ris-work