Home > Mobile >  Is there a way for me to use a List<string> property class with Entity Framework?
Is there a way for me to use a List<string> property class with Entity Framework?

Time:05-02

This is the class:

namespace backend
{
    [Table("Products")]
    public class Product
    {
        public long Id { get; set; }
        [Required]
        public string? Name { get; set; }

        public string? Category { get; set; }

        public string? Short { get; set; }

        public string? Description { get; set; }
        [Required]
        public float Price { get; set; }

        public string? MainImage { get; set; }

        public float  Disccount { get; set; }
        
        public string[]? Images { get; set; } // List<string>

    }
}

I've tried to run EF migrations but it appears that [] is not supported and, if I make it a List, it will ask for this List's key but it's just an array of strings

What am I doing wrong? Is not possible to add arrays as classes properties in EF?

CodePudding user response:

What's wrong with doing what a relational database would expect:

namespace backend
{
    [Table("Products")]
    public class Product
    {
        public long Id { get; set; }
        [Required]
        public string? Name { get; set; }

        public string? Category { get; set; }

        public string? Short { get; set; }

        public string? Description { get; set; }
        [Required]
        public float Price { get; set; }

        public string? MainImage { get; set; }

        public float  Disccount { get; set; }
        
        public ICollection<ProductImage>? Images { get; set; } //make it a new hashset in the constructor, btw

    }

    [Table("ProductImages")]
    public class ProductImage
    {
        public long Id { get; set; }
        [Required]
        public string? Name { get; set; }

        public string? Url { get; set; }

        public long ProductId { get; set; }
        
        public Product? Product { get; set; }

}

this way you can add more data to the image, like "front view", "side view", "version 2" etc, and EF can map it like it knows how to; as a separate table that is 1:M related

Is not possible to add arrays as classes properties in EF?

Technically, the ICollection<ProductImage> is a property that is an "array".. but out-of-the-box, not arrays of primitives, no

CodePudding user response:

It depends on the use case. Does the data be used in a relational child table way (1:many)? Or is it really just a list of some urls that don't have any further relations within the database?

For the first case, take a look at Caius answer. The second approach would be to write a type converter and register it within EF core. In that case your underlying type in the database would be some kind of string (e.g. nvarchar(max)) and EF core makes the conversion on the client side

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<Product>()
        .Property(e => e.Images)
        .HasConversion(
            v => JsonSerializer.Serialize(v),
            v => JsonSerializer.Deserialize<string[]>(v));
}

Instead of JSON you could also use some other approach like string.Join() and string.Split(), that's up to you. Further information can be found at Microsoft documentation.

  • Related