0

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.)

Jean-David Lanz
  • 865
  • 9
  • 18
  • Usually such things rely on database's capabilities so if your DB doesn't support default values then most probably you're out of luck. – montonero Jun 13 '22 at 13:07

1 Answers1

2

The HasDefaultValue only applies to the database so the EF object wouldn't have that value. I think it's not working because null is a valid value for string/VARCHAR. If you set the field to "Not Null" by using the IsRequired() method then it may work as expected when saving to the database e.g.

z_etbTable.Property(t => t.RegularString).HasColumnName("BT_RegularString")
                                             .HasColumnType("varchar(25)")
                                             .HasDefaultValue("Hello solar system!")
                                             .HasMaxLength(25)
                                             .IsRequired();
YungDeiza
  • 3,128
  • 1
  • 7
  • 32
  • OK, that's the straight answer I had come here for. I still don't see the point in specifying it in the code (except maybe to inform coders). – Jean-David Lanz Jun 13 '22 at 13:33
  • Specifying what, the "IsRequired" ? – YungDeiza Jun 15 '22 at 11:26
  • No, the default value. Since EF goes to the database to fetch it anyway, the only reason I can think of for specifying it in the code with `HasDefaultValue()` would be as information for coders. – Jean-David Lanz Jun 15 '22 at 15:27
  • It's a default database value so if you don't provide a value for that field the default that you specify is saved to the DB – YungDeiza Jun 15 '22 at 22:29
  • Thanks for your answer. I'm not sure I understand it fully, so here's a reworded question: what difference does it make whether `HasDefaultValue()` is specified or not? – Jean-David Lanz Jun 16 '22 at 06:02
  • ```HasDefaultValue()``` is used to specify a different default value for a field. If a row is inserted and no value/null is provided for the field that ```HasDefaultValue``` was called on, then the specified value will be stored to the Database. So in your case if null is passed as the value for BT_RegularString then ```"Hello solar system!"``` will be saved to the DB instead of ```null```. – YungDeiza Jun 16 '22 at 09:08
  • All right, I think I'm starting to get it. But what if `BT_RegularString` is defined *in the database* with a default value of "Hello solar system!" and `RegularString` doesn't have the `HasDefaultValue()` specification? Is `"Hello solar system!" still saved to the DB? – Jean-David Lanz Jun 16 '22 at 15:44
  • 1
    You've kind of described an impossible scenario if I'm understanding correctly (and you're using code first). ```HasDefaultValue``` is how you tell the database what default value to use for ```BT_RegularString```. – YungDeiza Jun 16 '22 at 22:07
  • We-ellll, the database definitely exists first in my project, and we're trying to slowly tack EFCore on. So I understand that in the case of a database-first scenario, `HasDefaultValue()` isn't needed in the `OnModelCreating()` method. (Other details, such as `HasMaxLength()` or `HasColumnName()`, range from useful to totally necessary.) Right? – Jean-David Lanz Jun 20 '22 at 08:59
  • 1
    Ah okay, ```HasDefaultValue()``` (as well as methods like ```HasMaxLength()```) is really for code first. A comment explaining that there is a default value of x for a given property is better than using ```HasDefaultValue()``` on a database first project. ```HasColumnName()``` is useful if you want to give properties a different name from the database fields. – YungDeiza Jun 20 '22 at 11:11
  • At last we reach mutual understanding. Apologies for not making things clearer from the onset, and thanks a lot for sticking around through that long discussion! – Jean-David Lanz Jun 21 '22 at 12:25
  • No problem, I glad I could eventually help. – YungDeiza Jun 21 '22 at 12:32