有没有办法让 Entity Framework Core 将所有 Guid 属性映射到 nvarchar 而无需注释?

Is there a way to make Entity Framework Core map all Guid properties to nvarchar without annotations?

我有一个使用 Guids 作为 primary/foreign 键的领域模型。有没有一种方法可以指示 ModelBuilder 为这些属性显式创建具有 nvarchar 存储的数据库表,而无需在我的实体中的每个现有主键和外键上应用注释 类?

使用 Microsoft.EntityFrameworkCore.Storage.ValueConversion.GuidToStringConverter 值转换器。

var guidToStringConverter = new GuidToStringConverter();

modelBuilder.Entity<YourEntity>()
    .Property(ye => ye.PropertyThatIsAGuid)
    .HasConversion(guidToStringConverter);