Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map System.Uri using Entity Framework Fluent Api

Pretty simple question. I have a model that has a property which is a System.Uri type. Uris don't have a default parameterless constructor, and no ID field. Is there any way to override my model generation to store it in the DB in a custom way (e.g. just as a string)? In NHibernate, I've done this before by implementing IUserType, but I could not find yet a similar mechanism in CodeFirst.

Obviously, I could create a custom type that uses a Uri under the hood and exposes regular mappable properties & constructor, I'm just curious if there's any way to map this system type so that I don't have to make a wrapper like that.

like image 284
Paul Avatar asked Feb 18 '11 02:02

Paul


People also ask

Why we use Fluent API with Entity Framework?

Fluent API is another way to configure your domain classes. The Code First Fluent API is most commonly accessed by overriding the OnModelCreating method on your derived DbContext. Fluent API provides more functionality for configuration than DataAnnotations.

What is Fluent API in Entity Framework?

Entity Framework Fluent API is used to configure domain classes to override conventions. EF Fluent API is based on a Fluent API design pattern (a.k.a Fluent Interface) where the result is formulated by method chaining. In Entity Framework Core, the ModelBuilder class acts as a Fluent API.

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.

How do I add a foreign key in Fluent API?

You can then configure foreign key properties by using the HasForeignKey method. This method takes a lambda expression that represents the property to be used as the foreign key.


3 Answers

This is a very old question but I just had the same question today. With Entity Framework Core 2.1, you can set up a Value Conversion:

public class MyEntityDbConfiguration : IEntityTypeConfiguration<MyEntity>
{
    public void Configure(EntityTypeBuilder<MyEntity> builder)
    {
        builder.Property(e => e.UriField)
                .HasConversion(v => v.ToString(), v => new Uri(v));
    }
}

public class MyDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new MyEntityDbConfiguration());
    }
}
like image 143
dstj Avatar answered Oct 22 '22 04:10

dstj


EF does not support custom type mappings like NH.

For System.Uri in particular, I'd use a wrapper property and map the actual value as a string; it's not that bad.

like image 20
Diego Mijelshon Avatar answered Oct 22 '22 05:10

Diego Mijelshon


Unfortunately, there is no direct way to map System.Uri to string with EF.

You could however use Data Annotations and attribute your URL property as follows:

[DataType(DataType.Url)]
public string Link { get; set; }

This will able to tell some services that this should be displayed and validated as a URL (ASP.NET and Silverlight for instance have built in support for it).

like image 5
Shimmy Weitzhandler Avatar answered Oct 22 '22 04:10

Shimmy Weitzhandler