在 Entity Framework Core 中使用 Fluent API 编写范围数据注释
Writing Range Data Annotation with Fluent API in Entity Framework Core
描述
因为我正在尝试采用 Clean Architecture,所以我想摆脱 Data Annotations 并使用 流利 API 就个人而言,我开始更喜欢它。
不过,我不太擅长这种实现方式。
范围注释(旧方法)
我之前所做的是使用 Range
注释为数字属性指定 min 和 max像这样:
public class Engine
{
...
[Range(0, 10000)]
public int Size { get; set; }
[Display(Name = "Horse power")]
[Range(0, 1000)]
public int HorsePower { get; set; }
[Display(Name = "Top speed")]
[Range(0, 500)]
public int? TopSpeed { get; set; }
...
}
配置class(更简洁的方法)
现在,我正在尝试在与 Engine
实体关联的 Configuration class 中编写验证代码。
实体class:
public class Engine
{
public int EngineId { get; set; }
public int Size { get; set; }
public int HorsePower { get; set; }
public int? TopSpeed { get; set; }
public int FuelId { get; set; }
public int TransmissionId { get; set; }
}
这是我目前的配置class:
using Carsurfer.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Carsurfer.Persistence.Configurations
{
public class EngineConfiguration : IEntityTypeConfiguration<Engine>
{
public void Configure(EntityTypeBuilder<Engine> builder)
{
builder.Property(engine => engine.Size);
}
}
}
问题
Entity Framework 核心是否内置了一些东西?
我应该以某种方式实现 IValidatableObject
接口吗?
配置class是否应该关注其实体的验证?
Is there something built in to Entity Framework Core for this?
据我所知,没有 Fluent API 等同于 RangeAttribute
。
Should I implement the IValidatableObject interface somehow?
实施 IValidatableObject
不会让您在 RangeAttribute
中使用 Fluent API。您仍然需要使用 DataAnnotation
属性。
Should the Configuration class be concerned with the validation of its
entity?
IEntityTypeConfiguration
旨在提供一种干净的方式来分离您的 DbModel
配置以供 EntityFrameworkCore
使用。因此,通常此类配置 class 将包含架构级别设置和验证(约束、最大值、关系配置(在适当的情况下))。
您需要问自己的问题是,您为什么要首先使用 RangeAttribute
。它对模式没有任何影响。您可能正在使用它,因为您还在内置验证中将实体 classes 用于 MVC。如果是这种情况,并且您共享您的实体 (POCO) classes 而不是为您的 ViewModel 或 Dto 使用单独的 classes,那么您将不得不使用混合版本,在该版本中您将数据注释分开EF Core 与 IEntityTypeConfiguration
classes 相关,并将用于 MVC 验证目的的数据注释保留为属性。值得注意的是,就 EF Core 而言,DisplayNameAttribute
也是无关紧要的。
描述
因为我正在尝试采用 Clean Architecture,所以我想摆脱 Data Annotations 并使用 流利 API 就个人而言,我开始更喜欢它。
不过,我不太擅长这种实现方式。
范围注释(旧方法)
我之前所做的是使用 Range
注释为数字属性指定 min 和 max像这样:
public class Engine
{
...
[Range(0, 10000)]
public int Size { get; set; }
[Display(Name = "Horse power")]
[Range(0, 1000)]
public int HorsePower { get; set; }
[Display(Name = "Top speed")]
[Range(0, 500)]
public int? TopSpeed { get; set; }
...
}
配置class(更简洁的方法)
现在,我正在尝试在与 Engine
实体关联的 Configuration class 中编写验证代码。
实体class:
public class Engine
{
public int EngineId { get; set; }
public int Size { get; set; }
public int HorsePower { get; set; }
public int? TopSpeed { get; set; }
public int FuelId { get; set; }
public int TransmissionId { get; set; }
}
这是我目前的配置class:
using Carsurfer.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Carsurfer.Persistence.Configurations
{
public class EngineConfiguration : IEntityTypeConfiguration<Engine>
{
public void Configure(EntityTypeBuilder<Engine> builder)
{
builder.Property(engine => engine.Size);
}
}
}
问题
Entity Framework 核心是否内置了一些东西?
我应该以某种方式实现
IValidatableObject
接口吗?配置class是否应该关注其实体的验证?
Is there something built in to Entity Framework Core for this?
据我所知,没有 Fluent API 等同于 RangeAttribute
。
Should I implement the IValidatableObject interface somehow?
实施 IValidatableObject
不会让您在 RangeAttribute
中使用 Fluent API。您仍然需要使用 DataAnnotation
属性。
Should the Configuration class be concerned with the validation of its entity?
IEntityTypeConfiguration
旨在提供一种干净的方式来分离您的 DbModel
配置以供 EntityFrameworkCore
使用。因此,通常此类配置 class 将包含架构级别设置和验证(约束、最大值、关系配置(在适当的情况下))。
您需要问自己的问题是,您为什么要首先使用 RangeAttribute
。它对模式没有任何影响。您可能正在使用它,因为您还在内置验证中将实体 classes 用于 MVC。如果是这种情况,并且您共享您的实体 (POCO) classes 而不是为您的 ViewModel 或 Dto 使用单独的 classes,那么您将不得不使用混合版本,在该版本中您将数据注释分开EF Core 与 IEntityTypeConfiguration
classes 相关,并将用于 MVC 验证目的的数据注释保留为属性。值得注意的是,就 EF Core 而言,DisplayNameAttribute
也是无关紧要的。