使用实体框架设置C#中的数据类型

发布于 2025-01-28 08:22:05 字数 754 浏览 4 评论 0原文

我在C#代码中有此枚举:

enum MyOptionsEnum
{
   Option1,
   Option2,
   Option3
};

我有一个使用此枚举的实体:

class OrderEntity
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    ...
    public MyOptionsEnum Options { get; set; }
}

如果我想为每个订单选择一个选项,则此代码正常。

我要做的是允许用户检查1,2或3个选项(或0)。这就是我所说的“一组”选项。

这就是我所做的:

enum MyOptionsEnum
{
   Option1 = 1,
   Option2 = 2,
   Option3 = 4
};

如果我想按特定顺序设置option1和option3,这就是我在代码中所做的事情:

order.Options = MyOptionsEnum.Option1 | MyOptionsEnum.Option3;

它有效,但对我来说很难。

让我们想象另一个开发人员在枚举中添加了一个新选项,而忘记在值中设置了2个倍数。它行不通...

我的问题:C# / Entity Framework中是否有“集合”数据类型?

谢谢

I have this enum in my C# code:

enum MyOptionsEnum
{
   Option1,
   Option2,
   Option3
};

I have an entity which uses this enum:

class OrderEntity
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    ...
    public MyOptionsEnum Options { get; set; }
}

This code works fine if I want to choose ONE option for each order.

What I want to do is to allow user to check 1,2 or 3 options (or 0). This is what I call a "set" of options.

Here is what I've done:

enum MyOptionsEnum
{
   Option1 = 1,
   Option2 = 2,
   Option3 = 4
};

And here is what I've done in my code if i want to set Option1 and Option3 on a specific order:

order.Options = MyOptionsEnum.Option1 | MyOptionsEnum.Option3;

It works but it is ugly for me.

Let's imagine another developer which adds a new option in the enum and forget to set a multiple of 2 in the value. It won't work...

My question: is there a "Set" datatype in C# / Entity Framework ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文