I have a nullable varchar(max)
column in SQL Server that I'm mapping to a Guid?
in EF code-first. However, this property is actually in a base class that many other entities derive from.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Model1>().Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
}
The above line is repeated many times for each table. Is there a way to tell EF that this is a base class property so the mapping can be declared only once?
Sure it is possible. With the lack of custom conventions, it is achieved with the "typical"
modelBuilder.Model.GetEntityTypes()
loop. Something like this (just change the base class and the property names):You may also consider using the EF Core provided out of the box
Guid
toString
converter: