【发布时间】:2016-06-05 12:50:50
【问题描述】:
我有一个映射到数据库表的模型,我在该模型中有一个枚举属性。我在表格列中存储了一个int 值,并使用枚举来管理事物(因为我有一个小的修复列表)。
我的问题是我想要特定 db 列值的枚举文本。
public class Item
{
public int ItemId { get; set; } // ItemId (Primary key)
[Required]
[DisplayName("Category")]
public int? CategoryId { get; set; } // CategoryId
[Required]
public string Name { get; set; } // Name (length: 100)
[DisplayName("Item Code")]
public string ItemCode { get; set; } // ItemCode (length: 15)
[DisplayName("Unit Type")]
public int? UnitId { get; set; } // UnitId
public DateTime? UpdatedOnUtc { get; set; } // UpdatedOnUtc
public DateTime? CreatedOnUtc { get; set; } // CreatedOnUtc
[DisplayName("Is Active")]
public bool IsActive { get; set; } // IsActive
// Reverse navigation
public virtual ICollection<GatePass> GatePasses { get; set; }
public virtual ICollection<MoveOrderItem> MoveOrderItems { get; set; }
public virtual ICollection<ProjectItemPrice> ProjectItemPrices { get; set; }
// Foreign keys
public virtual Category Category { get; set; }
[NotMapped]
public Enums.UnitType UnitType { get; set; }
}
我将枚举值存储在 UnitId 列中,为了显示它的值,我想显示该枚举/枚举显示属性的文本。
我的枚举是UnitType:
public enum UnitType
{
Set = 1,
Km = 2,
No = 3,
Kg = 4,
Mtr = 5
}
我的主要目的是管理 EF 查询中的显示,因为在 EF 列表中我会有 UnitId 但我想显示 UnitType 的单位文本,如 kg、set、no、mtr 等。
我经常在 SQL 中使用case when as 做这些事情,但我不知道如何在 EF 中进行管理。
【问题讨论】:
标签: c# asp.net-mvc entity-framework enums