使用EF6中的LINQ填充子查询

时间:2018-03-29 17:47:23

标签: c# asp.net entity-framework-6

我正在尝试使用Entity Framework 6创建一个带有LINQ查询的.json文件:

   {
    "id": "1231-12321-sdff-21-31",
    "name": "Product name",
    "description": "Product description"
    "rating": 4.1,
    "price": 7.50,
    "photos": 
    [
     "http://path/photo1.img",
     "http://path/photo2.img"
    ]
}

如何填充子矢量photos?请参阅下面的代码:

我的选择代码

List<ProductViewModel> products = await
    (from prod in db.product
    join prodPhoto in db.product_photo on prod.id equals prodPhoto.product_id
    where prod.id == id
    select new ProductViewModel
    {
        id = prod.id,
        name = prod.name,
        description = prod.description,
        rating = prod.rating,
        price = prod.price,
        photos = new HashSet<ProductPhotoViewModel>()
        {
            new ProductPhotoViewModel
            {
                path = prodPhoto.path
            }
        }
    }).ToListAsync();

我的对象ProductViewModel

    public class ProductViewModel
    {
    public ProductViewModel()
    {
        photos = new HashSet<ProductPhotoViewModel>();
    }

    //Product
    public string id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public int? rating { get; set; }
    public decimal price { get; set; }

    //Product photo
    public virtual ICollection<ProductPhotoViewModel> photos { get; set; }
}

和我的对象ProductImageViewModel

    public class ProductPhotoViewModel
    {
        public string path { get; set; }
    }

我知道我的选择可能是错的,但我看不出我能做些什么来解决这个问题。

修改

EF6课程:

产品

    public partial class product
    {
    public product()
    {
        this.employee_change_product = new HashSet<employee_change_product>();
        this.offer = new HashSet<offer>();
        this.order_has_product = new HashSet<order_has_product>();
        this.product_photo = new HashSet<product_photo>();
        this.product_has_category = new HashSet<product_has_category>();
        this.product_stock = new HashSet<product_stock>();
    }

        public string id { get; set; }
        public string description { get; set; }
        public Nullable<int> rating { get; set; }
        public Nullable<int> preparation_time { get; set; }
        public decimal price { get; set; }
        public string name { get; set; }
        public string establishment_id { get; set; }
        public System.DateTime create_time { get; set; }
        public System.DateTime update_time { get; set; }

        public virtual ICollection<employee_change_product> employee_change_product { get; set; }
        public virtual establishment establishment { get; set; }
        public virtual ICollection<offer> offer { get; set; }
        public virtual ICollection<order_has_product> order_has_product { get; set; }
        public virtual ICollection<product_photo> product_photo { get; set; }
        public virtual ICollection<product_has_category> product_has_category { get; set; }
        public virtual ICollection<product_stock> product_stock { get; set; }
    }

产品照片

    public partial class product_photo
    {
        public string id { get; set; }
        public Nullable<int> width { get; set; }
        public Nullable<int> height { get; set; }
        public int size { get; set; }
        public string path { get; set; }
        public string product_id { get; set; }
        public System.DateTime create_time { get; set; }
        public System.DateTime update_time { get; set; }

        public virtual product product { get; set; }
    }

非常感谢!

0 个答案:

没有答案