I guess that's a silly question, but I can't seem to find a straight answer.
I'm working with an EF Core entity, BanalTable
. It has a property, RegularString
, that I've set a default value in the OnModelCreating()
method like so:
z_etbTable.Property(t => t.RegularString).HasColumnName("BT_RegularString")
.HasColumnType("varchar(25)")
.HasDefaultValue("Hello solar system!")
.HasMaxLength(25);
In my code, I create a BanalTable
object with a simple
BanalTable z_objKnownData = new BanalTable();
Debugging it shows that z_objKnownData.RegularString
is null
, while I'd expect it to be "Hello solar system!"
I've also tried to add it to the repository:
z_dbcContext.BanalTableRepository.Add(z_objKnownData);
Both z_objKnownData
and z_dbcContext.BanalTableRepository.Local[0]
show a null
value for RegularString
.
It bears mentioning that for now, our database uses SQLServer 11, so it doesn't support default values. However, an upgrade might be in the works, so I'd like my code to be as ready as it can.
So... am I right to expect RegularString
to be set to the default value I specified in OnModelCreating()
? Or is the default value supposed to be set in the data row when the repository's SaveChanges()
, and only then set in the entity? (If so, what the blessed knowledge is the point with specifying a default value in OnModelCreating()
?)
(I just found that the "Note:" in this answer apparently answers my question, albeit sideways as it wasn't the main focus on the original question. Then I read the answers to this question and they seem to imply that no, the default value isn't automatically set at object creation, but don't say it out loud. Also, both are more than five years old, a long time in computer science, and could now be outdated on that point.)