Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save an enumeration value to a database?

For example:

namespace PizzaSoftware.Data
{
    public class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public Permission PermissionType { get; set; }
    }

    public enum Permission
    {
        Basic,
        Administrator
    }
}

If if I were to use Entity-Framework's CodeFirst, how can I save this value?

This is for a Windows Forms, Desktop application.

Thank you!


1 Answers

You can retrieve the int value (or whatever type you have your enum set to) via a simple cast and save that to the DB.

And to read it back out, you'd cast from int back to enum:

Permission enumValue = (Permission)intValue;
like image 97
Kon Avatar answered Nov 18 '25 20:11

Kon