I have a columns FlgStdTest
which is DEFAULT ((1))
and datatype is bit
.
I'm trying to insert the record with FlgStdTest
as true
or false
, but its value is always inserted as true
.
public async Task<int> CreateTest(TestDetailsDTO input)
{
int testId = await InsertAndGetIdAsync(ObjectMapper.Map<TestMaster>(input));
return testId;
}
[AutoMap(typeof(TestMaster))]
public class TestDetailsDTO : FullAuditedEntityDto
{
public string Test { get; set; }
public string TestDesc { get; set; }
public bool FlgStdTest { get; set; }
public bool FlgActive { get; set; }
}
[Table("TestMaster")]
public class TestMaster: FullAuditedEntity
{
public const int NVarcharLength20 = 20;
public const int NVarcharLength50 = 50;
[Required]
[MaxLength(NVarcharLength20)]
public virtual string Test { get; set; }
[Required]
[MaxLength(NVarcharLength50)]
public virtual string TestDesc { get; set; }
[Required]
public virtual bool FlgStdTest { get; set; }
[Required]
public virtual bool FlgActive { get; set; }
}
modelBuilder.Entity<TestMaster>(b =>
{
b.HasIndex(e => new { e.Test, e.IsDeleted }).IsUnique();
b.Property(e => e.FlgActive).HasDefaultValue(true);
b.Property(e => e.FlgStdTest).HasDefaultValue(true);
});
Request:
{
"testType": "abc",
"testDesc": "xyz",
"flgStdtest": false,
"flgActive": false,
"isDeleted": false,
"deleterUserId": 0,
"deletionTime": "2017-10-05T10:50:13.956Z",
"lastModificationTime": "2017-10-05T10:50:13.956Z",
"lastModifierUserId": 0,
"creationTime": "2017-10-05T10:50:13.956Z",
"creatorUserId": 0,
"id": 0
}
Thanks Aaron for your suggestion, it has really helped. Read the below paragraph carefully.
The 'bool' property 'FlgStdTest' on entity type 'TestMaster' is configured with a database-generated default. This default will always be used when the property has the value 'false', since this is the CLR default for the 'bool' type. Consider using the nullable 'bool?' type instead so that the default will only be used when the property value is 'null'.
I have found a simple workaround to solve my problem, only thing needs to be done is declaring bool
member as nullable
.
[AutoMap(typeof(TestMaster))]
public class TestDetailsDTO : FullAuditedEntityDto
{
public string Test { get; set; }
public string TestDesc { get; set; }
public bool? FlgStdTest { get; set; }
public bool? FlgActive { get; set; }
}
[Table("TestMaster")]
public class TestMaster: FullAuditedEntity
{
public const int NVarcharLength20 = 20;
public const int NVarcharLength50 = 50;
[Required]
[MaxLength(NVarcharLength20)]
public virtual string Test { get; set; }
[Required]
[MaxLength(NVarcharLength50)]
public virtual string TestDesc { get; set; }
[Required]
public virtual bool? FlgStdTest { get; set; }
[Required]
public virtual bool? FlgActive { get; set; }
}