How can NodaTime be used with EF Code First?

前端 未结 3 2216
-上瘾入骨i
-上瘾入骨i 2020-12-04 15:56

I really want to be able to use NodaTime in my Entity Framework Code First database projects but haven\'t found a \"clean\" way to do it. What I really want to do is this:<

3条回答
  •  爱一瞬间的悲伤
    2020-12-04 16:36

    EF Core 2.1 has a new feature Value Conversions, which is exactly for this scenario.

    //OnModelCreating
    builder.Entity
           .Property(e => e.SomeInstant)
           .HasConversion(v => v.ToDateTimeOffset(), v => Instant.FromDateTimeOffset(v));
    

    .HasConversion has some other overloads to make this logic re-useable, for example you can define your own ValueConverter.

提交回复
热议问题