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

前端 未结 10 493
死守一世寂寞
死守一世寂寞 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 17:58

    Its simple

    public class Person
    {
        [Required]
        [MaxLength(50)]
        public string FirstName { get; set; }
    
        [Required]
        [MaxLength(50)]
        public string LastName { get; set; }
    
        [Required]
        public DateTime DateOfBirth { get; set; }
    
        public string AddressesJson { get; set; }   //save this json properity in table.
    
        [NotMapped]
        public List
    Addresses { get => !string.IsNullOrEmpty(AddressesJson) ? JsonConvert.DeserializeObject>(AddressesJson) : null; } } public class Address { public string Type { get; set; } public string Company { get; set; } public string Number { get; set; } public string Street { get; set; } public string City { get; set; } }

提交回复
热议问题