2

I have quite big enum with custom numbers like

 public enum DataSources
    {
        Undefined = 0,
        [Description(nameof(Resources.Web))]
        Web = 1,
        [Description(nameof(Resources.Mail))]
        Email = 2, 
        [Parent(Value = (int)Other)]
        [Description(nameof(Resources.VoIP))]
        Voip = 5,
        [Parent(Value = (int)Other)]
        [Description(nameof(Resources.ProcessAndApplications))]
        Processes = 257,
        ...

And now I'm looking a way to store multiple DataSources (enum above) values in another class let's call it Role (it's db entity represents row in database).

class Role : DbEntity
{
        [PrimaryKey, Identity]
        public override long Id { get; set; }
        [Column(Name = "name"), NotNull]
        public string Name { get; set; }
        public DataSources dataSources {get;set;}
} 

Usually I could use [Flags] but I can't change already assigned values for DataSources enum. Is there any solution in this case? May be I could use additional attributes for bit values?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ilya Sulimanov
  • 7,636
  • 6
  • 47
  • 68
  • I've add some details hope it's clearly now – Ilya Sulimanov Mar 13 '20 at 06:36
  • Hrm hack... store a list of comma separated values, create a non-mapped property to extract and build the list. Or just create another table – TheGeneral Mar 13 '20 at 06:40
  • 1
    Found something here that might help (if you are using EF on .net core). I could not find the same feature for EF on .net framework though https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions – Oguz Ozgul Mar 13 '20 at 06:43
  • @MichaelRandall I hope you're not suggesting to store [comma-separated values in a database....](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – Zohar Peled Mar 13 '20 at 07:11
  • @ZoharPeled, haha my options would be bitflags or another table, however who am i to judge :) – TheGeneral Mar 13 '20 at 07:37

1 Answers1

0

You could store multiple values in a single column by using bitwise OR. But this requires that all enum values occupy unique bits. I can already see this is not going to be possible in your case because you have more than one odd number.

The next option is to extract the DataSources column into its own table and add a FK to it from Role. Your Role class changes as shown below.

class Role : DbEntity
{
    [PrimaryKey, Identity]
    public override long Id { get; set; }
    [Column(Name = "name"), NotNull]
    public string Name { get; set; }
    public IList<RoleDataSources> DataSources {get;set;}
}

class RoleDataSources : DbEntity
{
    [PrimaryKey, Identity]
    public override long Id { get; set; }
    [ForeignKey("Role"), Identity]
    public long RoleId { get; set; }
    public Role Role { get; set; }
    public DataSources Value { get; set; }
}
Pranav Negandhi
  • 1,594
  • 1
  • 8
  • 17