如何使用 Entity Framework 核心在具有默认值的布尔值上设置另一个值?
How to set another value on a boolean with defaut value with Entity Framework Core?
我在这个问题中遇到了完全相同的问题:
像他一样,我从客户端到控制器得到了我的布尔值的好值,false,但是由于 Entity Framework 和默认值,它被 _context.SaveChanges();
的调用设置为 true数据库中的约束。
但是:我正在使用 Entity Framework 核心,我没有任何 [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
注释可以删除来解决问题。
在我的 ApplicationDbContext.cs 中:
modelBuilder.Entity<myEntity>(entity =>
{
entity.Property(e => e.Active).HasDefaultValueSql("1");
//entity.Property(e => e.Active).HasDefaultValueSql<bool>("1"); // doesn't fix
...
}
在我的数据库中:
CREATE TABLE myEntity(
Id INTEGER IDENTITY(1,1) NOT NULL,
...
Active BIT NOT NULL CONSTRAINT DF_myEntity_Active DEFAULT 1
);
在我的控制器中:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id, Active, etc.")] Entity myEntity)
{
if (ModelState.IsValid)
{
_context.Add(myEntity); // here myEntity.Active = false
await _context.SaveChangesAsync();
// same problem with _context.SaveChanges(); in another controller
// here myEntity.Active = true
}
...
}
似乎 EF 没有正确映射 C# 布尔值与 SQL 位,并且始终采用默认值。有人有强制使用错误值的问题吗?
最后,我可以使用 EF 命令中的 --data-annotations
选项在我的 EF 模型中生成数据注释。所以我把 [DatabaseGenerated(DatabaseGeneratedOption.None)]
放在 属性 上,不使用数据库的默认约束。
我也遇到过与可为 null 的 bool 值几乎类似的问题。我在 Sqlite 数据库之上使用 EF。对我来说,更新值 true/false 没有反映在数据库中。在我重写 OnModelCreating 方法并配置 属性 如下
之后
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.Property(p => p.IsActive).ValueGeneratedNever().HasDefaultValue(null);
}
在此更改之后,值已按预期更新。希望这对你也有帮助。
在我自己研究了这个问题后,我认为您无法使用 annotations. However, I have found a rather tricky workaround 设置默认值。
如果您将 bool 值设置为可为空,然后使用流畅的 api 设置默认值,您应该没问题。它并不完美,但它有效:
public class Year
{
public int Id { get; set; }
public string label { get; set; }
public int year { get; set; }
public bool? active { get; set; }
}
然后,在您的数据上下文中,设置默认值:
modelBuilder.Entity<Year>()
.Property("active")
.HasDefaultValue(true);
当您将新记录插入数据库时,您不需要在对象声明中指定布尔值 属性。下面,2017 年的默认值为 true。
var newYears = new List<Year>();
newYears.Add(new Year { label = "2019", year = 2019, active = false });
newYears.Add(new Year { label = "2018", year = 2018, active = true });
newYears.Add(new Year { label = "2017", year = 2017});
_context.Years.AddRange(newYears);
_context.SaveChanges();
我在这个问题中遇到了完全相同的问题:
像他一样,我从客户端到控制器得到了我的布尔值的好值,false,但是由于 Entity Framework 和默认值,它被 _context.SaveChanges();
的调用设置为 true数据库中的约束。
但是:我正在使用 Entity Framework 核心,我没有任何 [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
注释可以删除来解决问题。
在我的 ApplicationDbContext.cs 中:
modelBuilder.Entity<myEntity>(entity =>
{
entity.Property(e => e.Active).HasDefaultValueSql("1");
//entity.Property(e => e.Active).HasDefaultValueSql<bool>("1"); // doesn't fix
...
}
在我的数据库中:
CREATE TABLE myEntity(
Id INTEGER IDENTITY(1,1) NOT NULL,
...
Active BIT NOT NULL CONSTRAINT DF_myEntity_Active DEFAULT 1
);
在我的控制器中:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id, Active, etc.")] Entity myEntity)
{
if (ModelState.IsValid)
{
_context.Add(myEntity); // here myEntity.Active = false
await _context.SaveChangesAsync();
// same problem with _context.SaveChanges(); in another controller
// here myEntity.Active = true
}
...
}
似乎 EF 没有正确映射 C# 布尔值与 SQL 位,并且始终采用默认值。有人有强制使用错误值的问题吗?
最后,我可以使用 EF 命令中的 --data-annotations
选项在我的 EF 模型中生成数据注释。所以我把 [DatabaseGenerated(DatabaseGeneratedOption.None)]
放在 属性 上,不使用数据库的默认约束。
我也遇到过与可为 null 的 bool 值几乎类似的问题。我在 Sqlite 数据库之上使用 EF。对我来说,更新值 true/false 没有反映在数据库中。在我重写 OnModelCreating 方法并配置 属性 如下
之后 protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.Property(p => p.IsActive).ValueGeneratedNever().HasDefaultValue(null);
}
在此更改之后,值已按预期更新。希望这对你也有帮助。
在我自己研究了这个问题后,我认为您无法使用 annotations. However, I have found a rather tricky workaround 设置默认值。
如果您将 bool 值设置为可为空,然后使用流畅的 api 设置默认值,您应该没问题。它并不完美,但它有效:
public class Year
{
public int Id { get; set; }
public string label { get; set; }
public int year { get; set; }
public bool? active { get; set; }
}
然后,在您的数据上下文中,设置默认值:
modelBuilder.Entity<Year>()
.Property("active")
.HasDefaultValue(true);
当您将新记录插入数据库时,您不需要在对象声明中指定布尔值 属性。下面,2017 年的默认值为 true。
var newYears = new List<Year>();
newYears.Add(new Year { label = "2019", year = 2019, active = false });
newYears.Add(new Year { label = "2018", year = 2018, active = true });
newYears.Add(new Year { label = "2017", year = 2017});
_context.Years.AddRange(newYears);
_context.SaveChanges();