Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are an Entity Framework object's properties with HasDefaultValue() automatically set to said default values at creation?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading