Skip to content

gojanpaolo/UnitsNet.EntityFrameworkCore

Repository files navigation

UnitsNet.EntityFrameworkCore

Functions/Methods to easily integrate UnitsNet with EntityFrameworkCore. Currently, this only has Value Conversions. Feel free to open a new issue if there are features we can add to this library.

Install

Run the following command in your Package Manager Console:

install-package UnitsNet.EntityFrameworkCore

Value Conversion

builder.Property(x => x.MyLength).HasConversion(LengthUnit.Meter);

Full Sample:

using Microsoft.EntityFrameworkCore;
using UnitsNet;
using UnitsNet.EntityFrameworkCore;
using UnitsNet.Units;

public class FooContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Foo>(builder =>
        {
            builder.Property(x => x.MyLength).HasConversion(LengthUnit.Meter);
            builder.Property(x => x.MySpeed).HasConversion(SpeedUnit.KilometerPerHour);
        });
    }
}

public class Foo
{
    public Length MyLength { get; set; }
    public Speed MySpeed { get; set; }
}