EF Core Data 注释必须大于其他字段
EF Core Data annotation Must be greater than an other field
这是模型 class 生成 table。
public class Mission
{
[Key]
public string Id { get; set; }
[Range(minimum: 1, maximum: int.MaxValue)]
public int MinDayRate { get; set; }
[Range(minimum: 1, maximum: int.MaxValue)]
public int MaxDayRate { get; set; }
}
在 SQL 服务器中:
更改 TABLE 任务添加约束 CK_MaxDayRate_vs_MinDayRate
CHECK (MinDayRate <= MaxDayRate );
我如何创建数据注释 MaxDayRate 必须大于 MinDayRate 以创建约束,如我的 sql 服务器示例?
There is a data annotation [Compare]
. However, [Compare]
checks that the two properties are equal, not that one is larger than the other. There does exist EFCore.CheckConstraints which applies various .NET validation attributes as database check constraints.
更好的方法是在 OnModelCreating:
中使用精确的 SQL 定义任何检查约束
modelBuilder.Entity<MyModel>()
.HasCheckConstraint("CK_Properties_MinDayRate_MaxDayRate", "[MaxDayRate] > [MinDayRate]");
这是模型 class 生成 table。
public class Mission
{
[Key]
public string Id { get; set; }
[Range(minimum: 1, maximum: int.MaxValue)]
public int MinDayRate { get; set; }
[Range(minimum: 1, maximum: int.MaxValue)]
public int MaxDayRate { get; set; }
}
在 SQL 服务器中: 更改 TABLE 任务添加约束 CK_MaxDayRate_vs_MinDayRate CHECK (MinDayRate <= MaxDayRate );
我如何创建数据注释 MaxDayRate 必须大于 MinDayRate 以创建约束,如我的 sql 服务器示例?
There is a data annotation
[Compare]
. However,[Compare]
checks that the two properties are equal, not that one is larger than the other. There does exist EFCore.CheckConstraints which applies various .NET validation attributes as database check constraints.
更好的方法是在 OnModelCreating:
中使用精确的 SQL 定义任何检查约束modelBuilder.Entity<MyModel>()
.HasCheckConstraint("CK_Properties_MinDayRate_MaxDayRate", "[MaxDayRate] > [MinDayRate]");