EF Core 1.0 not including columns with default values in insert into query
See original GitHub issueSteps to reproduce
I’m using EF Core 1.0 and I have a model where I design some properties with default values similar to
modelBuilder.Entity<Listings>(entity => { entity.Property(e => e.IsActive).HasDefaultValueSql("1");}
The table in the database is created correctly defining the columns with the corresponding default value
[IsActive] bit NOT NULL DEFAULT 1
The issue
When I add a new entity and save changes in my context, the insert into statement generated does not contain the properties that have default values even though I assign specific values for those properties. The result in the database is a record where the columns with default values doesn’t contain the values I passed in my model.
More info on this post http://stackoverflow.com/questions/40619319/entity-framework-not-including-columns-with-default-value-in-insert-into-query/
Further technical details
EF Core version: 1.0 Visual Studio version: VS 2015
Issue Analytics
- State:
- Created 7 years ago
- Comments:69 (22 by maintainers)
Just want to weigh in as this issue has just stung us. Unsure why it has been closed. We have the same scenario - added a default value of true to a boolean property so that when the migration was applied existing records would get the value of true.
Completely unexpected behaviour that this would make it impossible to set false when inserting a record.
Using V5.0.5 and this is still an issue. If it’s not possible to set a default value that is not the same as the C# default for that type then why is the method offered? We had an int property where our DB default was set to -1. With this bug it meant that any time we tried to set it to 0, it was stored in the DB as -1.