fengling-member-service/.github/instructions/entity-configuration.instructions.md

71 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
applyTo: "src/Fengling.Member.Infrastructure/EntityConfigurations/*.cs"
---
# 实体配置开发指南
## 开发原则
### 必须
- **配置定义**
- 必须实现 `IEntityTypeConfiguration<T>` 接口。
- 每个实体一个配置文件。
- **字段配置**
- 必须配置主键,使用 `HasKey(x => x.Id)`
- 字符串属性必须设置最大长度。
- 必填属性使用 `IsRequired()`
- 所有字段都不允许为 null使用 `IsRequired()`
- 所有字段都必须提供注释说明。
- 根据查询需求添加索引。
- **强类型ID配置**
- 对于强类型 ID直接使用值生成器。
- `IInt64StronglyTypedId` 使用 `UseSnowFlakeValueGenerator()`
- `IGuidStronglyTypedId` 使用 `UseGuidVersion7ValueGenerator()`
### 必须不要
- **转换器**:不要使用 `HasConversion<{Id}.EfCoreValueConverter>()`,框架会自动处理强类型 ID 的转换。
- **RowVersion**`RowVersion` 无需配置。
## 文件命名规则
- 类文件应放置在 `src/Fengling.Member.Infrastructure/EntityConfigurations/` 目录下。
- 文件名格式为 `{EntityName}EntityTypeConfiguration.cs`
## 代码示例
**文件**: `src/Fengling.Member.Infrastructure/EntityConfigurations/UserEntityTypeConfiguration.cs`
```csharp
using Fengling.Member.Domain.AggregatesModel.UserAggregate;
namespace Fengling.Member.Infrastructure.EntityConfigurations;
public class UserEntityTypeConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("Users");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.UseGuidVersion7ValueGenerator()
.HasComment("用户标识");
builder.Property(x => x.Name)
.IsRequired()
.HasMaxLength(50)
.HasComment("用户姓名");
builder.Property(x => x.Email)
.IsRequired()
.HasMaxLength(100)
.HasComment("用户邮箱");
builder.HasIndex(x => x.Email)
.IsUnique();
}
}
```