如何使用DbSet<CustomEntity>
正确限制IEntityTypeConfiguration<CustomEntity>
保持单行?
目前我正在限制此实体存储库的GetAsync(int id)
方法仅返回第一行,并且在AddAsync(CustomEntity newEntity)
上,每当添加新记录时都要检查以确保DbSet<CustomEntity>
中只有一个。
这是我正在使用的IEntityTypeConfiguration<CustomEntity>
:
public class CustomEntityConfiguration : IEntityTypeConfiguration<CustomEntity>
{
public void Configure(EntityTypeBuilder<CustomEntity> builder)
{
// ID
builder.HasKey(e => e.Id);
builder.Property(e => e.Titulo).IsRequired().HasMaxLength(255);
builder.Property(e => e.Dth).IsRequired();
builder.Property(e => e.Resumo).HasMaxLength(2000);
builder.Property(e => e.Pago);
builder.Property(e => e.Publico);
builder.Property(e => e.LinkFacebook).HasMaxLength(255);
builder.Property(e => e.LinkWebsite).HasMaxLength(255);
builder.Property(e => e.LinkYouTube).HasMaxLength(255);
}
}
基本上,您需要通过存储库实现此目的。 EntityTypeConfiguration仅指定CustomEntity与数据库表之间的映射。您可以应用QueryFilters仅根据特定条件返回元素。但我建议在您的存储库中处理此业务逻辑。以下是如何使用它们的文档