【问题标题】:Return list of entities from web service从 Web 服务返回实体列表
【发布时间】:2012-12-06 09:39:55
【问题描述】:

我为我的模型使用实体框架(edmx 文件),并且我正在使用 WCF 来访问我的模型。 当尝试返回实体数据模型(示例表对象)列表时,出现错误:

服务器没有提供有意义的回复;这可能是由于合同不匹配、会话过早关闭或内部服务器错误造成的。

这是我的 WCF 代码。 IService.cs:

[ServiceContract(CallbackContract = typeof(IPesananCallBack), SessionMode = SessionMode.Required)]
public interface IPelayanWebService
{
   [OperationContract]
   List<table> GetAllTables();
}

Service.cs:

public List<table> GetAllTables()
{
    TableModel model = new TableModel();
    return model.GetAllTables();
}

我在模型中的代码:

public List<table> GetAllTables()
{
    using (restoicdbEntities ent = new restoicdbEntities())
    {
        return ent.table.ToList();
    }
}

在我的客户端中,我只是调用了该函数,然后发生了错误。 我必须创建自己的数据合同吗?有没有办法从 edmx 生成数据合约?

更新: 这是从 Entity Framework 生成的代码:

[EdmEntityTypeAttribute(NamespaceName="restoicdbModel", Name="table")]
[Serializable]
[DataContractAttribute(IsReference=true)]
public partial class table: EntityObject
{
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 ID_TABLE
    {
        get
        {
            return _ID_TABLE;
        }
        set
        {
            if (_ID_TABLE != value)
            {
                OnID_TABLEChanging(value);
                ReportPropertyChanging("ID_TABLE");
                ID_TABLE = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("ID_TABLE");
                OnID_TABLEChanged();
            }
        }
    }
    private global::System.Int32 ID_TABLE;
    partial void OnID_TABLEChanging(global::System.Int32 value);
    partial void OnID_TABLEChanged();
}

【问题讨论】:

标签: c# .net wcf entity-framework edmx


【解决方案1】:

阅读 Steve Wilkes 提供的链接WCF, Entity Framework & Data Contracts 后,似乎从 Web 服务传递实体对象并不好。所以我必须创建自己的数据传输对象,它是实体对象的映射,然后从网络服务传递它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2014-06-28
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多