Through examples in the past, I have learned to use DbSet<TEntity>.Create()
to create entity objects for insertion. However, I have found just using the default constructor with an object initializer seems to work just as well.
My main question is, do we ever still need to use DbSet<TEntity>.Create()
, and why?
Bonus question: What, if any, is the functional difference between these two code examples?
"By the book":
Dim db As New EFDAL.WidgetDbEntities()
Dim dbWidgetBatch2 As EFDAL.WidgetBatch = db.WidgetBatches.Create()
dbWidgetBatch2.CreationTimestamp = Now
dbWidgetBatch2.CreatedByUsername = userName
db.WidgetBatches.Add(dbWidgetBatch2)
db.SaveChanges()
"Works just as well?":
Dim db As New EFDAL.WidgetDbEntities()
Dim dbWidgetBatch As New EFDAL.WidgetBatch With {
.CreationTimestamp = Now,
.CreatedByUsername = userName
}
db.WidgetBatches.Add(dbWidgetBatch)
db.SaveChanges()