设置最小int值的约束 [英] Set a constraint for minimum int value

查看:124
本文介绍了设置最小int值的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Repository模式。我有一个名为Product的实体,我想设置价格的最小值以避免零价格。可以在EntitytypeConfiguration类中创建它吗?

I am using the Repository pattern. I have a entity called Product and I want to set the minimum value for price to avoid zero prices. Is it possible to create it in a EntitytypeConfiguration class?

我的产品配置类

 public class ProductConfiguration : EntityTypeConfiguration<Product>
 {
    public PlanProductConfiguration(string schema = "dbo")
    {
        ToTable(schema + ".Product");
        HasKey(x => new { x.IdProduct });

        Property(x => x.Price).HasColumnName("flt_price")
                              .IsRequired()
                              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
   }
}


推荐答案

服务器端验证,在您的实体上实现IValidatableObject。对于客户端验证,如果使用MVC,请将数据注释添加到视图模型中。并添加数据库约束,通过在迁移中调用Sql()方法来添加检查约束。

For server side validation, implement IValidatableObject on your entity. For client-side validation, if you are using MVC, add the data annotation to your view model. And to add a database constraint, add the check constraint by calling the Sql() method in a migration.

这篇关于设置最小int值的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆