【问题标题】:How to handle enum while transferring service layer to business layer?将服务层转移到业务层时如何处理枚举?
【发布时间】:2012-07-17 14:23:04
【问题描述】:

我在我的服务层中使用枚举。如果我设置枚举的值,反之亦然,一切都很好,我不会设置它的值,而不是它给我一个错误 错误:

The underlying connection was closed: The connection was closed unexpectedly.

我在 DataContract 类中使用了枚举,它将在数据库操作时使用。 我正在使用 WCF 服务通过数据模型与数据库连接。在某些方法中我使用的是枚举,但在某些方法中我不是。 DataContract 类:

[DataMember]
public Enums.SearchType SearchType { get; set; }

枚举声明:

 public enum SearchType
    {
      Search = 'S',

      Export = 'E',

      Undefined = 0
    }

那么在这种情况下我该怎么办?如果有人对此有任何想法,请帮助我...

提前谢谢............

【问题讨论】:

  • 我认为你在滥用枚举。值应该是整数。
  • @flem: 你的意思是我不能用字符串值?
  • 它们不是字符串,而是字符,可以转换为整数,因此可以编译
  • 我认为这个问题与枚举没有直接关系。其他原因导致错误。更改服务的配置以返回错误,以便您查看错误是什么。

标签: asp.net wcf entity-framework enums


【解决方案1】:

确保您的枚举类型具有默认值 (0)

public SearchType
{
   Undefined = 0,
   ...
}

枚举是 Int32(除非另有说明)。 default(Int32) 为 0。default(Enums.SearchType) 也将为 0。如果枚举中未定义 0,则数据合约反序列化将失败。

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 2014-04-13
    • 2011-12-06
    • 2011-06-16
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多