简体   繁体   中英

Entity Framework Field Enumeration

I am developing a Silverlight Application in VS 2010 with SQL Server 2008 as the database server. 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. Eg:- 0 = User, 1 = Moderator, 2 = Administrator, 3 = Super Administrator etc. The AccessLevel is stored as an int in the database. In the UI, I wish to display the list of users in a DataGrid control which can be edited by a DataForm control. The name of the access level must appear on the DataGrid as well as the DataForm instead of the level number. 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. Here's an example .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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