如何在 ASP.NET 样板中插入具有正确值的记录?
How to insert record with proper values in ASP.NET Boilerplate?
我有一个列 FlgStdTest
,它是 DEFAULT ((1))
,数据类型是 bit
。
我正在尝试将 FlgStdTest
的记录插入为 true
或 false
,但其值始终插入为 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);
});
要求:
{
"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
}
当您生成迁移时,应该会出现如下警告:
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'.
如果您希望在迁移数据库时为现有行设置默认值,但不希望 EF 对任何新插入的行使用默认值,您可以这样做:
modelBuilder.Entity<TestMaster>(b =>
{
b.HasIndex(e => new { e.Test, e.IsDeleted }).IsUnique();
b.Property(e => e.FlgActive)
.HasDefaultValue(true)
.ValueGeneratedNever();
b.Property(e => e.FlgStdTest)
.HasDefaultValue(true)
.ValueGeneratedNever();
});
参考:https://github.com/aspnet/EntityFrameworkCore/issues/9291
更新
If I don't pass FlgActive and FlgStdTest in request, it inserts false for both the columns, but columns are true by default.
使用自动属性初始化器:
public class TestDetailsDTO : FullAuditedEntityDto
{
public bool FlgStdTest { get; set; } = true;
public bool FlgActive { get; set; } = true;
}
public class TestMaster: FullAuditedEntity
{
public virtual bool FlgStdTest { get; set; } = true;
public virtual bool FlgActive { get; set; } = true;
}
感谢 Aaron 的建议,它真的很有帮助。请仔细阅读以下段落。
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'.
我找到了一个简单的解决方法来解决我的问题,唯一需要做的就是将 bool
成员声明为 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; }
}
我有一个列 FlgStdTest
,它是 DEFAULT ((1))
,数据类型是 bit
。
我正在尝试将 FlgStdTest
的记录插入为 true
或 false
,但其值始终插入为 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);
});
要求:
{
"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
}
当您生成迁移时,应该会出现如下警告:
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'.
如果您希望在迁移数据库时为现有行设置默认值,但不希望 EF 对任何新插入的行使用默认值,您可以这样做:
modelBuilder.Entity<TestMaster>(b =>
{
b.HasIndex(e => new { e.Test, e.IsDeleted }).IsUnique();
b.Property(e => e.FlgActive)
.HasDefaultValue(true)
.ValueGeneratedNever();
b.Property(e => e.FlgStdTest)
.HasDefaultValue(true)
.ValueGeneratedNever();
});
参考:https://github.com/aspnet/EntityFrameworkCore/issues/9291
更新
If I don't pass FlgActive and FlgStdTest in request, it inserts false for both the columns, but columns are true by default.
使用自动属性初始化器:
public class TestDetailsDTO : FullAuditedEntityDto
{
public bool FlgStdTest { get; set; } = true;
public bool FlgActive { get; set; } = true;
}
public class TestMaster: FullAuditedEntity
{
public virtual bool FlgStdTest { get; set; } = true;
public virtual bool FlgActive { get; set; } = true;
}
感谢 Aaron 的建议,它真的很有帮助。请仔细阅读以下段落。
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'.
我找到了一个简单的解决方法来解决我的问题,唯一需要做的就是将 bool
成员声明为 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; }
}