简体   繁体   English

实体框架字段枚举

[英]Entity Framework Field Enumeration

I am developing a Silverlight Application in VS 2010 with SQL Server 2008 as the database server. 我正在使用SQL Server 2008作为数据库服务器的VS 2010中开发Silverlight应用程序。 In a certain table in my database, I have a field AccessLevel which can take values such as 0, 1, 2 etc. each corresponding to a level of access. 在数据库的某个表中,我有一个AccessLevel字段,该字段可以采用0、1、2等值,每个值对应于访问级别。 Eg:- 0 = User, 1 = Moderator, 2 = Administrator, 3 = Super Administrator etc. The AccessLevel is stored as an int in the database. 例如:-0 =用户,1 =主持人,2 =管理员,3 =超级管理员,等等。AccessLevel作为int存储在数据库中。 In the UI, I wish to display the list of users in a DataGrid control which can be edited by a DataForm control. 在UI中,我希望在DataGrid控件中显示用户列表,该列表可由DataForm控件编辑。 The name of the access level must appear on the DataGrid as well as the DataForm instead of the level number. 访问级别的名称必须出现在DataGrid以及DataForm上,而不是级别编号上。 How can I achieve that? 我该如何实现? Entity Data Model and Domain Data Service are being used. 正在使用实体数据模型和域数据服务。 Thanks in advance. 提前致谢。

You could define an enum: 您可以定义一个枚举:

public enum AccessLevels
{
    User = 0,
    Moderator,
    Administrator,
    SuperAdministrator
}

Then create another property on your entity, that maps to the original access level: 然后在您的实体上创建另一个属性,该属性映射到原始访问级别:

public partial class CertainEntity
{
    public AccessLevelEnum AccessLevelValue
    {
        get { return (AccessLevels)AccessLevel; }
        set { AccessLevel = (int)value; }
    }
}

You can then use this property in DataGrids/DataForms. 然后,您可以在DataGrids / DataForms中使用此属性。 Here's an example . 这是一个例子

您可以编写一个Valueconverter并将其用于数据绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM