Description
In effort to reduce null
values in the codebase, I'm trying to use LanguageExt
library which provides Option<T>
type.
I've tried to map an entity with such property to the nullable column and got an error:
The property 'Entity.Title' cannot be marked as nullable/optional because the type of the property is 'Option' which is not a nullable type. Any property can be marked as non-nullable/required, but only properties of nullable types can be marked as nullable/optional.
The error sounds reasonable, since EFCore doesn't have any special treatment for the Option<T>
type. So I've tried to add conversion to string?
assuming that EFCore will see string?
as a type that should be mapped to data storage, however, the error remains the same.
Is there any reason for the original property's CLR Type being used in that check?
Is there any workarounds for this issue?
Any extension points in EFCore I can leverage to handle the case?
The repro sample is following console app (repro project attached):
using LanguageExt;
using Microsoft.EntityFrameworkCore;
Console.WriteLine("Hello, World!");
class DataContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer();
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity>(builder =>
{
builder.HasKey(e => e.Id);
builder.Property(e => e.Id);
builder.Property(e => e.Title)
.HasConversion<string?>(
v => v.Match<string?>(x => x, () => null),
v => v)
.IsRequired(false)
.HasMaxLength(50);
});
base.OnModelCreating(modelBuilder);
}
}
public class Entity
{
public int Id { get; set; }
public Option<string> Title { get;set; }
}
EF Core version: 8.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Activity