WCF中的嵌套数据协定异常 - 无法将实体模型隐式转换为DataContract中定义的模型

时间:2014-11-07 05:29:49

标签: c# asp.net-mvc entity-framework wcf

问题陈述:

在我的MVC 4应用程序中,我有一个索引视图(列表视图),我将从多个表中提取数据。我能够将数据绑定到网格并获取值。现在我将转换从MVC控制器获取数据所涉及的方法转换为WCF方法,我在转换中遇到了问题,我是能够用我的控制器代码。以下是我一直在使用的代码:

工作代码:

控制器:

public ActionResult Index()
{
    var result = GetIndexData();
    return PartialView(result);
}

public IEnumerable<GlobalUpgradeModel> GetIndexData()
{
    return (from u in db.Upgrade.AsEnumerable()
            join a in db.Asset.AsEnumerable() on u.ID equals a.ID
            where a.AssetID == u.AssetID
            orderby u.AssetID
            select new GlobalModel()
            {
                assetModel = a,
                upgradeModel = u
            }).Distinct().ToList();
}

public class GlobalModel
{
    public UpgradeModel upgradeModel { get; set; }

    public AssetModel assetModel { get; set; }
}

在这里,我能够从模型中获取并绑定数据到视图。一切正常。

现在我已将其转换为WCF:

IService1.cs:

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetUpgradeIndexData")]
IEnumerable<GlobalUpgradeDC> GetUpgradeIndexData();

[DataContract]
public class UpgradeDC
{
    [DataMember]
    public int AssetID { get; set; }

    [DataMember]
    public long SlNo { get; set; }

    [DataMember]
    public string UpgradeDetails{ get; set; }
}

[DataContract]
public class AssetDC
{
    [DataMember]
    public int AssetID { get; set; }

    [DataMember]
    public string AssetDescrip { get; set; }
}

[DataContract]
public class GlobalUpgradeDC
{
    [DataMember]
    public AssetDC assetDC { get; set; }

    [DataMember]
    public UpgradeDC upgradeDC { get; set; }
}

Service1.svc:

public IEnumerable<GlobalUpgradeDC> GetUpgradeIndexData()
{
    return (from u in dbEntity.UpgradeDetails.AsEnumerable()
            join a in dbEntity.AssetTaggings.AsEnumerable() on u.TenantID equals a.TenantID
            where a.AssetID == u.AssetID
            orderby u.AssetID
            select new GlobalUpgradeDC()
            {
                assetDC = a,
                upgradeDC = u

                //Here I'm getting error as Cannot Implicitly Convert type from Entities model to Model of data contract
            }).ToList();
}

我做错了什么?

0 个答案:

没有答案