特定の記事のコンポーネント データと言語データを取得する単純なクエリを作成し、それをビューモデルに入れたいと考えました。
ProductComponentテーブルはProductの子テーブルであり、その中の関連フィールドは ComponentID と ProductId (外部キー、parentId) であるため、ProductComponents をすべての言語固有のデータがある Product および ProductTranslations にリンクしたかったので、試してみました。特定の製品のコンポーネントのリストを取得するために、すべてを 1 つのクエリにまとめます。
クエリは次のとおりです。
public IEnumerable<ProductComponents> ListComponents(int productid, string language)
{
var query = (from c in context.ProductComponents
from ct in context.ProductTranslations
where ct.ProductId == c.ComponentId
where ct.ProductLanguage == language
from cp in context.Product
where cp.ProductId == c.ComponentId
where c.ProductId == productid
select new EnumComponents
{
ProductId = c.ComponentId,
Name = ct.ProductName,
SKU = cp.SKU
});
return query;
}
これにより、このエラーが発生し、返されたクエリが強調表示されます。一部も:
Cannot implicitly convert type 'System.Linq.IQueryable<Artikelhantering.Models.EnumComponents>' to 'System.Collections.Generic.IEnumerable<Artikelhantering.Models.ProductComponents>'. An explicit conversion exists (are you missing a cast?)
これがデータモデルのほとんどです。グーグルで見つけたものとスタックオーバーフローを調べた結果に基づいており、テーブル間の関係が原因である可能性があるため、そのほとんどを含めています。
public class Product
{
[Key]
public int ProductId { get; set; }
[DisplayName("Article nr")]
public string SKU { get; set; }
[DisplayName("Product Category")]
public int ProductCategoriesId { get; set; }
[DisplayName("Alternative Category")]
public int? AdditionalCategoriesId { get; set; }
[DisplayName("Show purchase button?")]
public bool Purchase { get; set; }
[DisplayName("Show Product?")]
public bool ShowProduct { get; set; }
[DisplayName("Picture name")]
public string Picture { get; set; }
[DisplayName("Is reference product?")]
public bool Reference { get; set; }
[DisplayName("Inprice")]
public decimal inPrice { get; set; }
[DisplayName("Additional costs")]
public decimal AddCost { get; set; } /
[DisplayName("Weight in kg")]
public decimal Weight { get; set; }
[DisplayName("Volume in m^3")]
public decimal Volume { get; set; }
[DisplayName("Vat code, use 0")]
public decimal VAT { get; set; }
public virtual IList<ProductTranslations> ProductTranslations { get; set; }
public virtual IList<ProductPrices> ProductPrices { get; set; }
public virtual IList<ProductComponents> ProductComponents { get; set; }
public virtual IList<ProductAccessories> ProductAccessories { get; set; }
public virtual ProductCategories ProductCategories { get; set; }
public virtual ProductCampaigns ProductCampaigns { get; set; }
}
public class ProductTranslations
{
[Key]
public int ProductTranslationsId { get; set; }
public int ProductId { get; set; } // This one links to the Product class
[DisplayName("Language Code")]
public string ProductLanguage { get; set; }
[DisplayName("Description")]
public string Description { get; set; }
[DisplayName("Product Name")]
public string ProductName { get; set; }
[MaxLength(255)]
[DisplayName("Meta Keywords")]
public string MetaKeywords { get; set; }
[MaxLength(255)]
[DisplayName("Meta Description")]
public string MetaDescription { get; set; }
public virtual Product Product { get; set; }
}
public class ProductComponents
{
[Key]
public int ProductComponentsId { get; set; }
public int ProductId { get; set; }
public int ComponentId { get; set; }
public virtual IList<ProductTranslations> ProductTranslations { get; set; }
public virtual Product Product { get; set; }
}
次に、モデル間の関係を次のように定義します。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<ProductCategories>()
.HasMany(x => x.ProductCategoriesTranslations) // Categories has many Translations
.WithRequired(y => y.ProductCategories) // Translations require a category
.HasForeignKey(p => p.ProductCategoriesId);
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductPrices) // Product has many ProductPricings
.WithRequired(y => y.Product) // ProductPricing has required Product
.HasForeignKey(p => p.ProductId);
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductTranslations) // Product has many ProductTranslations
.WithRequired(y => y.Product) // ProductTranslations has required Product
.HasForeignKey(p => p.ProductId);
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductComponents) // Product has many ProductComponents
.WithRequired(y => y.Product) // ProductComponents has required Product
.HasForeignKey(p => p.ProductId);
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductAccessories) // Product has many ProductAccessories
.WithRequired(y => y.Product) // ProductAccessories has required Product
.HasForeignKey(p => p.ProductId);
}
ProductComponents から ProductTranslations および Product への適切な関係を定義する必要があると推測していますが、その方法がよくわかりません。ProductComponents -> ProductTranslations の間の関係を作成するさまざまな方法を試しましたが、成功しませんでした。もちろん、問題は別のものかもしれません。