How to store JSON in an entity field with EF Core?

前端 未结 10 491
死守一世寂寞
死守一世寂寞 2020-11-29 17:13

I am creating a reusable library using .NET Core (targeting .NETStandard 1.4) and I am using Entity Framework Core (and new to both). I have an entity class that looks like

10条回答
  •  旧巷少年郎
    2020-11-29 18:12

    For those using EF 2.1 there is a nice little NuGet package EfCoreJsonValueConverter that makes it pretty simple.

    using Innofactor.EfCoreJsonValueConverter;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata.Builders;
    
    public class Campaign
    {
        [Key]
        public Guid Id { get; set; }
    
        [Required]
        [MaxLength(50)]
        public string Name { get; set; }
    
        public JObject ExtendedData { get; set; }
    }
    
    public class CampaignConfiguration : IEntityTypeConfiguration 
    {
        public void Configure(EntityTypeBuilder builder) 
        {
            builder
                .Property(application => application.ExtendedData)
                .HasJsonValueConversion();
        }
    }
    

提交回复
热议问题