HasDefaultValue() overwrites provided value when adding new entity
File a bug
According to the documentation it should work that I can setup default value for a property, but default value is ignored when I provide a value. It seem this is not the case.
I'm using:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.9"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.9"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.9"/>
Remarks
- I can't decide whether SQLite issue or not, I haven't tried it with Postgres
- If you use Rider, its unit test explorer sometimes don't see tests generated by Specflow, a rebuild might help; I don't know how VS/ReSharper behaves
- don't know whether it worked with 3.1.8 as this part of the app wasn't tested before
- I haven't tried with .net-5
Repro steps
Scenario 1
- Checkout the code this commit has the default value provided. The code is on the
feature/sourceformat_tree_mgmt_p2withHasDefaultValue() issuemessage. - solution file is in the
srcdirectory - build it
- run the following test:
DigitalLibrary.MasterData.BusinessLogic.Implementations.Tests.DimensionStructure.ListingDimensionStructuresFeature.GetListOfActiveDimensionStructures.Get list of active DimensionStructuresThere are 4 tests here (they are generated by Specflow) and 3 of them going to fail, 1 pass.
SQL output which shows that during INSERT the IsActive field is ignored.
And I have 1 inactive saved DimensionStructures Executed DbCommand (7ms) [Parameters=[@p0='pjfa' (Nullable = false) (Size = 4), @p1=NULL, @p2='dba598bf-8eed-4f1e-b15a-77dacc1644b8' (DbType = String), @p3='rxqc' (Nullable = false) (Size = 4), @p4='0' (DbType = String)], CommandType='Text', CommandTimeout='30'] INSERT INTO "dimension_structure" ("description", "dimension_id", "guid", "name", "sort_number") VALUES (@p0, @p1, @p2, @p3, @p4); SELECT "id", "is_active" FROM "dimension_structure" WHERE changes() = 1 AND "rowid" = last_insert_rowid();
Scenario 2
- the code is there
- Comment out the default value configuration of the entity. Entity configuration can be found here.
- Run any of the 3 failing tests from the previous scenario
- test passes
SQL output showing that during INSERT the IsActive field is not ignored.
And I have 1 inactive saved DimensionStructures Executed DbCommand (7ms) [Parameters=[@p0='yzec' (Nullable = false) (Size = 4), @p1=NULL, @p2='016e01b7-2b04-4edd-8728-575caf746ffb' (DbType = String), @p3='0' (DbType = String), @p4='bjry' (Nullable = false) (Size = 4), @p5='0' (DbType = String)], CommandType='Text', CommandTimeout='30'] INSERT INTO "dimension_structure" ("description", "dimension_id", "guid", "is_active", "name", "sort_number") VALUES (@p0, @p1, @p2, @p3, @p4, @p5); SELECT "id" FROM "dimension_structure" WHERE changes() = 1 AND "rowid" = last_insert_rowid();
@SayusiAndo I got to the point of running the tests in your repo, but I didn't see any test failures. Should I expect to see failures in the tests? Also, is logging already configured in your project?
@SayusiAndo I got to the point of running the tests in your repo, but I didn't see any test failures. Should I expect to see failures in the tests? Also, is logging already configured in your project?
I apologise for the late reply and thank you for taking a look at it. I'm swamped by other things, let me get back to you at the weekend and clarify what I wanted to say.
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
I had the same problem. Property with HasDefaultValue overriders the provided value. To make it more interesting the unit testing with a in memory database works fine. Any idea?