【问题标题】:WCF Data Service based on EF5 Model; how to add a custom type基于EF5模型的WCF数据服务;如何添加自定义类型
【发布时间】:2013-01-24 17:42:16
【问题描述】:

我正在尝试使用返回自定义类型的 ServiceMethod 构建 WCF 数据服务。 这种类型被用作一个容器来一次传输多个数据集合。我无法将此类型定义为实体或复杂类型。

public class BrfPackageDataContainer {
  public ICollection<BrfFlight> Flights {
    get;
    set;
  }

  public ICollection<BrfFlight_Info> Flight_Infos {
    get;
    set;
  }

  public ICollection<BrfInfo> Infos {
    get;
    set;
  }

  public BrfPackageDataContainer() {
    this.Flights = new List<BrfFlight>();
    this.Flight_Infos = new List<BrfFlight_Info>();
    this.Infos = new List<BrfInfo>();
  }
}

这是我的 ServiceMethod 声明:

    [WebGet]
    [SingleResult]
    public FlightInfoEntities.BrfPackageDataContainer GetBrfPackage () {
        var brfPackageDataContainer = new FlightInfoEntities.BrfPackageDataContainer();
        brfPackageDataContainer.Demodata();
        return brfPackageDataContainer;
    }

当我使用一个空的虚拟 DataService 作为服务类定义的数据源时,我运行了这个。但是当我使用我的实体框架模型作为数据源时,由于缺少自定义类型的元数据,服务拒绝启动。 我的问题是: 如何使用 EF 模型作为数据源并且仍然使用我的自定义类型作为我的方法的返回值。

【问题讨论】:

  • 我认为不可能。使用 Entity Framework 时,只能使用 EF 模型中定义的类型,并且不能将自定义类型添加到模型中。如果复杂类型对你来说足够了,你可以尝试在你的 EF 模型中添加一个虚拟复杂类型,EF 不会使用它,但你会使用它(警告 - 我没有尝试过)。

标签: entity-framework wcf service


【解决方案1】:

问题已通过解决方法解决:

我在模型中添加了 3 种复杂类型,与每个单独结果集的数据结构相匹配。 此外,我在数据上下文之外添加了一个容器类,它使用复杂类型将数据保存在一个对象中。 我使用自定义方法扩展了上下文类来处理存储过程调用并将结果映射到适当的复杂类型。ObjectContext.Translate 有很大帮助...... WCF 数据服务类使用虚拟 DataContext 进行实例化。这可以为我的自定义数据容器类创建元数据,现在可以将其用作自定义 WCF 数据服务方法的返回类型。 调用方法时会实例化数据上下文。

数据容器类` public class BrfPackageDataContainer { 公共指导交易 ID { 得到; 放; }

    public List<BrfFlight> Flights {
        get;
        set;
    }

    public List<BrfFlight_Info> Flight_Infos {
        get;
        set;
    }

    public List<BrfInfo> Infos {
        get;
        set;
    }

    public BrfPackageDataContainer () {
        this.Flights = new List<BrfFlight>();
        this.Flight_Infos = new List<BrfFlight_Info>();
        this.Infos = new List<BrfInfo>();
    }
}`

上下文扩展: 公共部分类 FlightInfoEntities { 公共虚拟 BrfPackageDataContainer GetBrfPackage(int?crewId,字符串 operatorCode,字符串离开,int?flightId,DateTime?stdRangeStart, 约会时间? stdRangeEnd,字符串 requestingApplication,字符串 requestingComputerName, string requestingACReg, ref Guid transactionId, int?具体信息类型ID,字节?详细程度, 布尔?跳过日志){ 使用 (DbCommand 命令 = this.Database.Connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "[dbo].[GetBrfPackage]";

            ...

            var dataContainer = new BrfPackageDataContainer();

            try {
                this.Database.Connection.Open();

                using (DbDataReader reader = command.ExecuteReader()) {
                    dataContainer.Flights = ((IObjectContextAdapter)this).ObjectContext.Translate<BrfFlight>(reader).ToList();
                    reader.NextResult();
                    dataContainer.Flight_Infos = ((IObjectContextAdapter)this).ObjectContext.Translate<BrfFlight_Info>(reader).ToList();
                    reader.NextResult();
                    dataContainer.Infos = ((IObjectContextAdapter)this).ObjectContext.Translate<BrfInfo>(reader).ToList();
                }
                return dataContainer;
            } catch (Exception ex) {
                throw ex;
            }
        }

WCF 数据服务方法:

    [WebGet]
    [SingleResult]
    public BrfPackageDataContainer GetBrfPackage () {
        using (var brfCtx = new FlightInfoEntities()) {
            Guid transactionId = new Guid();
            var brfPackageDataContainer = brfCtx.GetBrfPackage(null,"4T",null,null,null,null,"test",Environment.MachineName,null,ref transactionId,null,3,false);
            return brfPackageDataContainer;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多