簡體   English   中英

WCF從WCF服務獲取實體

[英]WCF getting entity from WCF service

我有一個WCF服務庫和Widnows表單作為客戶端。 我有ADO.NET EF數據庫,我想列出所有產品(衣服)及其尺寸。 (關系1至許多)。

public partial class ProductsEntity
{
    public ProductsEntity()
    {
        this.Sizes = new HashSet<SizesEntity>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }

    public virtual ICollection<SizesEntity> Sizes{ get; set; }
}

這是我的數據合同:

[DataContract]
public class Products
{
    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public string Name{ get; set; }
    [DataMember]
    public decimal Price { get; set; }
    [DataMember]
    public virtual ICollection<SizesEntity> Sizes{ get; set; }

}

 [DataContract]
public class Sizes
{

    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public int Name { get; set; }
    [DataMember]
    public Nullable<int> Quantity { get; set; }
    [DataMember]
    public int ID_Product { get; set; }

    [DataMember]
    public virtual ProductsEntity Products { get; set; }

}

我在數據庫中沒有此功能,但是我為查詢添加了Products_with_sizes(我不確定這是處理它的好方法)

[DataContract]
public class Products_with_sizes
{
    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public decimal Price { get; set; }
    [DataMember]
    public int S { get; set; }
    [DataMember]
    public int M { get; set; }
    [DataMember]
    public int L { get; set; }
}

using (var context = new dbMagazynierEntities())
            {
                var q = (from p in context.Products
                            where p.Name.Contains(name) && p.Price>= Price_from && p.Price <= Price_to
                            join r in context.Sizes
                                on p.ID equals r.Prodcuts.ID
                                into sizes
                            select new
                            {
                                ID = p.ID,
                                Name= p.Name,
                                Price = p.Price,
                                S = sizes.Where(x => x.Name== 0).Sum(x => x.Quantity) ?? 0,
                                M = sizes.Where(x => x.Name== 1).Sum(x => x.Quantity) ?? 0,
                                L = sizes.Where(x => x.Name== 2).Sum(x => x.Quantity) ?? 0,
                            });
                odp = new List<Products_with_sizes>();
                foreach (var item in q)
                {
                    odp.Add(new Products_with_sizes{ ID = item.ID, Name= item.Name, Price = item.Price, S = item.S, M = item.M, L = item.L });
                }

所以知道我在客戶中使用這種方法會出現錯誤

                         wyn = context.SzukajProduktu(id, name.Text, price_from, price_to);

我得到:

Cannot implicitly convert type 'System.Collections.Generic.List<Magazynier2WindowsFormsApplication.ServiceReference1.MyServiceProducts_with_sizes>' to 'System.Collections.Generic.List<Magazynier2ServiceLibrary.MyService.Products_with_sizes>'   

通過查看異常,您似乎正在嘗試將由服務代理生成的類直接轉換為您自己創建的DTO。

即使這兩個類具有相同的名稱和屬性,但它們實際上是不同的(即,沒有共同的父代或接口),並且位於不同的命名空間中。

您應該編寫一個將代理生成的類顯式轉換為DTO類的方法,例如

List<Magazynier2ServiceLibrary.MyService.Products_with_sizes> TranslateProxyClassToDTO(List<Magazynier2WindowsFormsApplication.ServiceReference1.MyServiceProducts_with_sizes> input)
{
    // translate all items and their properties and return the translated list
}
public List<Prodcuts_with_sizes> SzukajProduktu(int id, string name, decimal price_from, decimal price_to)
    {
        List<Prodcuts_with_sizes> odp;
        if (id == -1) //when id is not given
        {
            using (var context = new dbMagazynierEntities())
            {
                var q = (from p in context.Products
                            where p.Name.Contains(name) && p.Price >= price_from && p.Price <= price_to
                            join r in context.Size
                                on p.ID equals r.Products.ID
                                into sizes
                            select new
                            {
                                ID = p.ID,
                                Name = p.Name,
                                Price = p.Price,
                                S = sizes.Where(x => x.Name == 0).Sum(x => x.Quantity) ?? 0,
                                M = sizes.Where(x => x.Name == 1).Sum(x => x.Quantity) ?? 0,
                                L = sizes.Where(x => x.Name == 2).Sum(x => x.Quantity) ?? 0,
                            });
                odp = new List<Prodcuts_with_sizes>();
                foreach (var item in q)
                {
                    odp.Add(new Prodcuts_with_sizes { ID = item.ID, Name = item.Name, Price = item.Price, S = item.S, M = item.M, L = item.L });
                }
                //dataGridView1.DataSource = q.ToList();
            }
            return odp;
        }
        else //when id is given
        {
            using (var context = new dbMagazynierEntities())
            {
                var q = (from p in context.Products
                            where p.ID == id
                            join r in context.Sizes
                                on p.ID equals r.Products.ID
                                into sizes
                            select new
                            {
                                ID = p.ID,
                                Name = p.Name,
                                Price = p.Price,
                                S = sizes.Where(x => x.Name == 0).Sum(x => x.Quantity) ?? 0,
                                M = sizes.Where(x => x.Name == 1).Sum(x => x.Quantity) ?? 0,
                                L = sizes.Where(x => x.Name == 2).Sum(x => x.Quantity) ?? 0,
                            });
                odp = new List<Prodcuts_with_sizes>();
                foreach (var item in q)
                {
                    odp.Add(new Prodcuts_with_sizes { ID = item.ID, Name = item.Name, Price = item.Price, S = item.S, M = item.M, L = item.L });
                }
            }
            return odp;

        }
    }


 using (var context = new MyInterfaceClient())
                {
                     wyn = context.SzukajProduktu(id, name.Text, price_from, price_to);
                    //return wyn;
                }

我通過更改解決了

    [OperationContract]
    List<WCFLIB.MyService.Products_with_sizes> SzukajProduktu(int id, string name, decimal price_form, decimal price_to);

[OperationContract]
    List<MyService.Products_with_sizes> SzukajProduktu(int id, string name, decimal price_form, decimal price_to);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM