Hello everyone. This is my Entity, which is stored in database:
public class Rule
{
[Key]
public int ruleId { get; set; }
public string name { get; set; }
public int[] categoryIds { get; set; }
}
I have a converters for it like this:
public class ArrayConverter : ValueConverter<int[], string>
{
public ArrayConverter()
: base(x => string.Join(";", x), x => x.Split(";", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray())
{
}
}
public class ArrayComparer : ValueComparer<int[]>
{
public ArrayComparer()
: base((c1, c2) => c1.SequenceEqual(c2), c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())), c => c.ToArray())
{
}
}
Converters are applied like this:
modelBuilder.Entity<Rule>(builder =>
{
builder.Property(x => x.categoryIds).HasConversion(new ArrayConverter());
builder.Property(x => x.categoryIds).Metadata.SetValueComparer(new ArrayComparer());
});
What I have tried:
And this works OK, on client side. I need somehow to utilize this property (
int[] categoryIds
) on server side instead. It is stored in a string column like this: 1;2;3
What is the best way I can achieve it using linq to sql?
I have tried EF.Property<string>(p, "categoryIds").Contains("1") but it fails with some error that ef.property is not used correctly. I need to run it on server side.
categoryIds are not tied to Rule object, they are system wide entities, used by all of other entities, so I cannot do this:
public ICollection<Category> categories {get;set;}
because category would need to have relation to Rule, and it cannot have such.