@David Thielen, Welcome to Microsoft Q&A, you could refer to the Microsoft Learning Value Conversions to do it.
Here is a code example you could refer to.
public class Website
{
public int Id { get; set; }
public string? Name { get; set; }
public Uri uri { get; set; }
}
public class MyContext:DbContext
{
public DbSet<Website> websites { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("");
}
}
public class MyEntityDbConfiguration : IEntityTypeConfiguration<Website>
{
public void Configure(EntityTypeBuilder<Website> builder)
{
builder.Property(e => e.uri)
.HasConversion(v => v.ToString(), v => new Uri(v));
}
}
I add an entity in the main method:
static void Main(string[] args)
{
Uri uri = new Uri("uri path");
MyContext context=new MyContext();
context.websites.Add(new Website() { Name = "web1", uri = uri });
context.SaveChanges();
}
After checking, the database could store the URI correctly:
Hope my solution could be helpful.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.