如何在 Entity Framework 中定义枚举?
How to define Enums in Entity Framework?
我担心我是否正确存储了枚举值?在我的 EventModel class 中,我将它们存储为 int Id。
如果您能想到更好的数据库设计,我将不胜感激?有什么我应该改变的吗?
事件模型:
public class EventModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Location { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public byte[] EventLogo { get; set; }
public string Description { get; set; }
public List<Ticket> Tickets { get; set; }
public int EventTypeId { get; set; }
public int EventTopicId { get; set; }
}
门票:
public class Ticket
{
public int EventId { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public bool Free { get; set; }
}
事件类型枚举:
public enum EventType
{
AppearanceOrSinging = 1,
Attraction = 2,
Conference = 3,
Gaming = 4,
Dinner = 5,
FestivalOrFair = 6,
SocialAndNetworking = 7,
Competition = 8,
Tour = 9,
Race = 10,
SeminarOrTalk = 12,
Workshop = 13,
Other = 99
}
事件主题枚举:
public enum EventTopic
{
BusinessAndProfessional = 1,
Sporting = 2,
CharityAndCause = 3,
Music = 4,
TravelAndOutdoor = 5,
FashionAndBeauty = 6,
ReligionAndSpirituality = 7,
Education = 8,
FoodAndDrinks = 9,
CommunityAndCulture = 10,
ComputingAndTechnology = 11,
Other = 99
}
Entity Framework 从版本 5 开始直接支持枚举。您可以简单地替换您的属性定义,如下所示:
public EventType EventType { get; set; }
public EventTopic EventTopic { get; set; }
见MSDN
我担心我是否正确存储了枚举值?在我的 EventModel class 中,我将它们存储为 int Id。
如果您能想到更好的数据库设计,我将不胜感激?有什么我应该改变的吗?
事件模型:
public class EventModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Location { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public byte[] EventLogo { get; set; }
public string Description { get; set; }
public List<Ticket> Tickets { get; set; }
public int EventTypeId { get; set; }
public int EventTopicId { get; set; }
}
门票:
public class Ticket
{
public int EventId { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public bool Free { get; set; }
}
事件类型枚举:
public enum EventType
{
AppearanceOrSinging = 1,
Attraction = 2,
Conference = 3,
Gaming = 4,
Dinner = 5,
FestivalOrFair = 6,
SocialAndNetworking = 7,
Competition = 8,
Tour = 9,
Race = 10,
SeminarOrTalk = 12,
Workshop = 13,
Other = 99
}
事件主题枚举:
public enum EventTopic
{
BusinessAndProfessional = 1,
Sporting = 2,
CharityAndCause = 3,
Music = 4,
TravelAndOutdoor = 5,
FashionAndBeauty = 6,
ReligionAndSpirituality = 7,
Education = 8,
FoodAndDrinks = 9,
CommunityAndCulture = 10,
ComputingAndTechnology = 11,
Other = 99
}
Entity Framework 从版本 5 开始直接支持枚举。您可以简单地替换您的属性定义,如下所示:
public EventType EventType { get; set; }
public EventTopic EventTopic { get; set; }
见MSDN