【发布时间】:2020-03-05 05:24:00
【问题描述】:
我有一个使用 Razor 页面的 ASP.NET Core 项目。我正在使用实体框架,但使用我自己的自定义实体配置,如下所示:
namespace Customer.Data.Relational.Configuration {
public sealed class CustomerConfiguration {
public static void Configure(EntityTypeBuilder<CustomerEntity> entity) {
entity.ToTable("customers")
.HasKey(k => k.Id)
.HasName("pk_customers");
entity.Property(p => p.Id)
.HasColumnName("customer_id")
.HasColumnType("bigint")
.ValueGeneratedOnAdd()
.IsRequired();
entity.Property(p => p.Code)
.HasColumnName("customer_code")
.HasColumnType("nvarchar(15)")
.IsRequired();
entity.Property(p => p.FirstName)
.HasColumnName("first_name")
.HasColumnType("nvarchar(100)")
.IsRequired();
entity.Property(p => p.LastName)
.HasColumnName("last_name")
.HasColumnType("nvarchar(100)")
.IsRequired();
entity.Property(p => p.EmailAddress)
.HasColumnName("email_address")
.HasColumnType("nvarchar(100)")
.IsRequired();
entity.Property(p => p.IsPremierCustomer)
.HasColumnName("premier_customer")
.HasColumnType("bit")
.IsRequired();
entity.HasIndex(k => k.Code)
.HasName("uq_customers")
.IsUnique();
entity.HasOne(o => o.CustomerType)
.WithMany()
.HasForeignKey(f => f.CustomerTypeId)
.OnDelete(DeleteBehavior.Restrict)
.HasConstraintName("fk_customers_customer_types");
}
}
}
这允许我的实体类没有任何形式的注释(让它们只是 POCO)。
namespace Customer.Entities {
public class CustomerEntity : IAuditableEntity {
public long Id { get; set; } = default;
public string Code {
get; set;
}
public string FirstName {
get; set;
}
public string LastName {
get; set;
}
public string EmailAddress {
get; set;
}
public bool IsPremierCustomer {
get; set;
}
}
}
我使用FluentValidations 来验证用户输入。我创建了自定义验证器类。在客户端,我启用了 FluentValidation 与 ASP.NET Core 的集成。在服务器端,我通过调用 FluentValidation API 手动验证。
问题 - 这种方法正确吗?有没有其他方法可以在客户端和服务器上重复使用验证?任何指针或参考表示赞赏。
谢谢。
【问题讨论】:
标签: c# entity-framework validation asp.net-core