Сохранение данных в базе postgresql в формате inet и cidr через EF Core
Есть приложение Asp.Net Core WebApi. Есть класс Entity, который содержит свойство типа IPAddress. Хочется его отобразить в базу postgresql в поле с типом "inet". Как описано в документации к библиотеке Npgslq это стандартное отображение. Мне еще необходимо создать уникальный составной индекс, содержащий это поле. В классе контекста для такого отображения использую такой код:
modelBuilder.Entity<Switch>()
.Property(e => e.Ipaddr)
.HasColumnType("inet");
Для индекса - такой код:
modelBuilder.Entity<Switch>()
.HasIndex(s => new { s.Region, s.Ipaddr }).IsUnique();
Если так запустить создание миграции, возникает ошибка "Property 'Switch.Ipaddr' cannot be used as a key because it has type 'IPAddress' which does not implement 'IComparable', 'IComparable' or 'IStructuralComparable'. Use 'HasConversion' in 'OnModelCreating' to wrap 'IPAddress' with a type that can be compared."
Есть добавить конвертер
public class IpAddressToInetConverter : ValueConverter<IPAddress, string>
{
public IpAddressToInetConverter(ConverterMappingHints mappingHints = null)
: base(
ip => ip.ToString(),
str => IPAddress.Parse(str),
mappingHints)
{}
}
modelBuilder.Entity<Switch>()
.Property(e => e.Ipaddr)
.HasConversion(new IpAddressToInetConverter())
.HasColumnType("inet");
то возникает ошибка
The 'string' property 'Switch.Ipaddr' could not be mapped to the database type 'inet' because the database provider does not support mapping 'string' properties to 'inet' columns. Consider mapping to a different database type or converting the property value to a type supported by the database using a value converter. See https://aka.ms/efcore-docs-value-converters for more information. Alternately, exclude the property from the model using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Подскажите, пожалуйста, что делаю не так. Понимаю, что создание индекса можно добавить в класс миграции в метод Up, но хотелось бы использовать стандартный подход.