如何将枚举值保存到数据库? [英] How can I save an enumeration value to a database?
本文介绍了如何将枚举值保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
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
}
}
如果我要使用Entity-Framework的CodeFirst,我该如何保存这个值?
If if I were to use Entity-Framework's CodeFirst, how can I save this value?
这是一个Windows窗体,桌面应用程序。
This is for a Windows Forms, Desktop application.
谢谢!
推荐答案
您可以检索int值(或任何类型您的枚举设置为)通过简单的转换并将其保存到数据库。
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.
要阅读它,您将从int返回到枚举:
And to read it back out, you'd cast from int back to enum:
Permission enumValue = (Permission)intValue;
这篇关于如何将枚举值保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文