1

私は Entities Framework Code First を使用しており、次の 2 つのエンティティがあります。

public class Product 
{
    public int ProductID { get; set; }     
    public decimal Price { get; set; }
    public int ProductCategoryID { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }

    [NotMapped]
    public int Quantity { get; set; }
} 

public class ProductStorage
{
  [Key]
  public int ProductId { get; set; }


  [ForeignKey("ProductId")]
  public virtual Product Product { get; set; }

  public int Quantity { get; set; }

}

プロパティProductで満たされたリストを取得できないようです。Quantity

私は試した

from pr in repository.Query<Product>()
   join st in repository.Query<ProductStorage>() on pr.ProductID equals st.Quantity
   select new Product()
   {
       ProductID = pr.ProductID,
        ....
       Quantity = st.Quantity

   };

しかし、それはうまくいきませんでした。

4

1 に答える 1

2

あなたJoinOn間違ったフィールドを行っています。これを試してください:

from pr in repository.Query<Product>()
join st in repository.Query<ProductStorage>() on pr.ProductID equals st.ProductID 
select new Product()
   {
   ProductID = pr.ProductID,
        ....
   Quantity = st.Quantity
   };

ただし、両方ともフィールドProductStorageProduct共有してProductIdいるため、次のように 1 対 1 の関係で関連付けることができます。

public class Product 
{
    public int ProductID { get; set; }     
    public decimal Price { get; set; }
    public int ProductCategoryID { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }
    public virtual ProductStorage Storage { get; set; }

    [NotMapped]
    public int Quantity { get { return this.Storage.Quantity; }  }
}
于 2013-05-24T07:51:55.550 に答える