Skip to content

anhgeeky/EfCoreJsonValueConverter

 
 

Repository files navigation

JSON value converter for Entity Framework Core 3.0+

Publish NuGet

Serializes object properties in database as JSON blobs using Entity Framework Core value converters.

Usage example

Installing:

PM> Install-Package Innofactor.EfCoreJsonValueConverter

Annotate model properties with the JsonField attribute:

public class Customer {
  [JsonField]
  public Address Address { get; set; }
}

public class Address {
  // ...
}

This will persist Address property in database as JSON.

For DbContext:

  protected override void OnModelCreating(ModelBuilder builder) {

    builder.Entity<Customer>();
    builder.AddJsonFields();        
    
  }

Alternatively, individual fields can be mapped with the HasJsonValueConversion method:

  protected override void OnModelCreating(ModelBuilder builder) {

    builder.Entity<Customer>()
      .Property(m => m.Address)
      .HasJsonValueConversion();
    
  }

Note on performance

EF Core requires that tracked objects implement deep cloning and comparison. This library includes default fallback implementations for cloning and comparing, but for large objects that default implementation may be inefficient. To improve performance, it is recommended to manually implement ICloneable and IEquatable interfaces for complex JSON serialized objects. This library will then make use of those interfaces.

public class Address : ICloneable, IEquatable<Address> {
  // ...
}

About

JSON ValueConverter for EF Core 3.0+

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%