On an ASP.NET application I have a flag enum as follows:
[Flags]
enum Target : int {
None = 0,
Group = 1,
Student = 2,
Professor = 4,
All = Group | Student | Professor
}
Can I save a value containing more than one item in a SQL Database Table using Entity Framework 6?
How would that be saved?
Thank You, Miguel
You can use the '|' (bitwise OR ) operator to mask multiple roles together in a single 64-bit integer, which you can store in the database in an integer field (a bigint ). RoleEnum userRoles = RoleEnum.
A flagged enum can be used to efficiently send and store a collection of boolean values. In a flagged enum, each value of the enum is assigned to a bit value. These must be bit values because each combination possible will be unique.
Use an Existing Enum from a Different Namespace If you already have an Enum type created in your code, then you can use that as a data type of any entity property. To use an existing Enum type, right click on Designer → Add New → Enum Type. Enter the Enum Type Name in the dialog box.
You can save a combination of flags as an int:
int myFlags = (int)(Target.Group | Target.Professor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With