将枚举存储到实体中的数据库[重复]

时间:2022-09-14 16:57:37

This question already has an answer here:

这个问题在这里已有答案:

i am trying to pass a dropdown selected value to database my datacontracts has the following code

我试图将下拉选择值传递给数据库我的datacontracts有以下代码

      public EnumTypes.RegardingObjectType RegardingObjectType { get; set; }
    public Guid RegardingObjectId { get; set; }

enumtypes.cs has the following

enumtypes.cs具有以下内容

    public class EnumTypes
{

    public enum RegardingObjectType
    {
        UnknownOrNone = 0,
        Account = 1,
        Cellsite = 2,

eventdal.cs has the following

eventdal.cs具有以下内容

       private t_Event MapEventToEntity(Event newevent, t_Event eventToBeChanged)
    {
   eventToBeChanged.RegardingObjectType = int.Parse(newevent.RegardingObjectType.ToString());

it builds fine but getting exception while running

它构建良好,但在运行时获得异常

3 个解决方案

#1


1  

Entity Framework 4 does not support using enums as column types. If you want to have native support for enums, you'll need to upgrade to Entity Framework 5.

实体框架4不支持将枚举用作列类型。如果您希望对枚举具有本机支持,则需要升级到Entity Framework 5。

If you absolutely have to use EF 4 (because of a project that's already been implemented in it), then I recommend that you go around it as so:

如果你绝对必须使用EF 4(因为已经在其中实现的项目),那么我建议你这样做:

public int RegardingObjectTypeAsInt { get; set; }

[NotMapped]
public EnumTypes.RegardingObjectType RegardingObjectType
{
  get { return (EnumTypes.RegardingObjectType) this.RegardingObjectTypeAsInt; }
  set { this.RegardingObjectTypeAsInt = (int)value; }
}

It's not a pretty solution, but it's a solid workaround to the problem, and you still get to bind to it on your pages and treat it as the enum itself in your other code.

这不是一个漂亮的解决方案,但它是解决问题的一个可靠的解决方案,你仍然可以在你的页面上绑定它并在其他代码中将其视为枚举。

#2


0  

this worked

这很有效

eventToBeChanged.RegardingObjectType = (int)newevent.RegardingObjectType;

instead of

代替

  eventToBeChanged.RegardingObjectType = int.Parse(newevent.RegardingObjectType.ToString());

#3


0  

If you want to map the string value to the database, check out this answer:

如果要将字符串值映射到数据库,请查看以下答案:

How to map an enum property in Entity Framework 4

如何在Entity Framework 4中映射枚举属性

As @IronMan84 pointed out though, I'm not sure if this works with multi-word enums - but it's useful and it works none-the-less.

正如@ IronMan84指出的那样,我不确定这是否适用于多字枚举 - 但它很有用并且它可以正常工作。

#1


1  

Entity Framework 4 does not support using enums as column types. If you want to have native support for enums, you'll need to upgrade to Entity Framework 5.

实体框架4不支持将枚举用作列类型。如果您希望对枚举具有本机支持,则需要升级到Entity Framework 5。

If you absolutely have to use EF 4 (because of a project that's already been implemented in it), then I recommend that you go around it as so:

如果你绝对必须使用EF 4(因为已经在其中实现的项目),那么我建议你这样做:

public int RegardingObjectTypeAsInt { get; set; }

[NotMapped]
public EnumTypes.RegardingObjectType RegardingObjectType
{
  get { return (EnumTypes.RegardingObjectType) this.RegardingObjectTypeAsInt; }
  set { this.RegardingObjectTypeAsInt = (int)value; }
}

It's not a pretty solution, but it's a solid workaround to the problem, and you still get to bind to it on your pages and treat it as the enum itself in your other code.

这不是一个漂亮的解决方案,但它是解决问题的一个可靠的解决方案,你仍然可以在你的页面上绑定它并在其他代码中将其视为枚举。

#2


0  

this worked

这很有效

eventToBeChanged.RegardingObjectType = (int)newevent.RegardingObjectType;

instead of

代替

  eventToBeChanged.RegardingObjectType = int.Parse(newevent.RegardingObjectType.ToString());

#3


0  

If you want to map the string value to the database, check out this answer:

如果要将字符串值映射到数据库,请查看以下答案:

How to map an enum property in Entity Framework 4

如何在Entity Framework 4中映射枚举属性

As @IronMan84 pointed out though, I'm not sure if this works with multi-word enums - but it's useful and it works none-the-less.

正如@ IronMan84指出的那样,我不确定这是否适用于多字枚举 - 但它很有用并且它可以正常工作。