0

In EF + Fluent API, I can set a default as:

modelBuilder.Entity<Campaign>()
            .Property(b => b.Enabled)
            .HasDefaultValue(true);

However, generally the row in the table is created by creating an empty object, populating it with the values from the web page, then inserting that object into the database, thereby overriding and defaults set in SQL Server.

So, what is the Microsoft recommended way to do this? Because I think any defaults need to be set in the class declaration or the constructor.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • You mention a row of data, seemingly Campaign?, but then you show the code for a property. Are you curious about default values for rows or properties? – Travis J Mar 10 '23 at 22:34
  • @TravisJ The default value for Campaign.Enabled. It needs to default to true. TIA – David Thielen Mar 11 '23 at 01:28
  • I see. For a single property such as a boolean `Enabled` property, there are just so many different ways to accomplish defaulting that to true. However, there aren't really any direct recommendations because of how broad the tooling is for EF. There are so many "it depends" situations to examine in order to get that perfect. You could do it from code, in the constructor as your note; you could use a custom db schema for it; you could have the DbContext do it behind the scenes, etc. – Travis J Mar 11 '23 at 07:56
  • I would personally suggest making it a single bit in MSSQL, renaming it to Disabled, and allowing the natural course of the field default to take place, which is 0, thus false -- this is inline with your intent of having the default value for objects be enabled (or not disabled). This requires a minor tweak in some logic where you need to check for !Disabled instead of simply Enabled. It also benefits from being able to be used after the fact on new objects, as modifying a table with this property will simply default all values to Disabled = 0, or by relation Enabled. Just my 2 cents here. – Travis J Mar 11 '23 at 07:58
  • Here is also a related post on this: [Entity Framework 6 Code first Default value](https://stackoverflow.com/questions/19554050/entity-framework-6-code-first-default-value) – Travis J Mar 11 '23 at 08:01
  • @TravisJ That was a great link - giant discussion of this. The thing is, reading all that I think the fundamental issue remains, whatever value is in the object, that will be written to the DB on an INSERT. So all the work to create a default value set in the database is for naught because that default is overwritten. Thank you for the comments & link. – David Thielen Mar 11 '23 at 19:48
  • Yes, that is true. If you are always going to be ensuring that value exists when SaveChanges is used, then the defaults don't really play a large role. Often, defaults on the database side (or configuration side) are used for calculated fields, migrations or for modifying existing tables. – Travis J Mar 12 '23 at 18:02

0 Answers0