2016-03-09 42 views
0

在我的MySQL數據庫中,我有三個表,其中,Item和innerItems都有圖像。因此,在圖像錶行中,innerItem_id或itemId不爲空,但不是兩者。C#LINQ查詢外鍵循環

CREATE TABLE `Item` (
    `itemId` int(11) NOT NULL AUTO_INCREMENT, 
    `name` longtext, 
    `description` longtext, 
    PRIMARY KEY (`itemId`)) 

CREATE TABLE `InnerItem` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` longtext, 
    `description` longtext, 
    `itemId` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `FK_InnerItem_Item_itemId` FOREIGN KEY (`itemId`) REFERENCES `Item` (`itemId`) 
) 

CREATE TABLE `Images` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `url` longtext, 
    `innerItem_id` int(11) DEFAULT NULL, 
    `itemId` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `FK_Images_Item_itemId` FOREIGN KEY (`itemId`) REFERENCES `Item` (`itemId`), 
    CONSTRAINT `FK_Images_InnerItem_innerItem_id` FOREIGN KEY (`innerItem_id`) REFERENCES `Products` (`id`) 
) 

我使用C#LINQ查詢數據庫,

var records = db.items.Where (p => p.name== name).Select (entity => 
         new { 
          id = entity.itemId, 
          name = entity.name, 
          description = entity.description, 
          images = entity.images.Select (img => new 
           { 
            id = img.id, 
            url = img.url 
           }), 
          innerItems= entity.innerItems.Select(item => new { 
           id = item.id, 
           description = item.description, 
           name = item.name, 
           images = item.images.Select (img => new 
            { 
             id = img.id, 
             url = "images" + "/" + img.uuid + ".jpg", 
             uuid = img.uuid 
            }), 
          }) 

         }).ToList(); 

當exceuting LINQ查詢,我得到以下錯誤,

Error    - System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: Unknown column 'Join3.itemId' in 'on clause' 

對此有何想法?

UPDATE

數據模型類

public class Item{ 
     public Item() { 
      images = new List<Image>(); 
      innerItems= new List<InnerItem>(); 
     } 

     [Key] 
     public int itemId{get;set;} 
     public string name {get;set;} 
     public string description {get;set;} 
     public virtual ICollection<Image> images {get;set;} 
     public virtual ICollection<InnerItem> innerItems{get;set;} //Has many 

      } 
public class InnerItem{ 
     public InnerItem() { 
      images = new List<Image>(); 
     } 

     [Key] 
     public int id {get;set;} 
     public string name {get;set;} 
     public string description {get;set;} 
     public virtual ICollection<Image> images {get;set;} 

     public virtual Item item{get;set;} //many-to-one 

    } 

public class Image { 
     [Key] 
     public int id {get;set;} 
     public string url {get;set;} 
     //Either one 
     [ForeignKey("item")] 
     public int? itemId{ get; set;} 
     [ForeignKey("itemId")] 
     public virtual Item item{get;set;} //many-to-one 
     [ForeignKey("innerItem")] 
     public int? innerItem_id { get; set;} 
     [ForeignKey("innerItem_id")] 
     public virtual InnerItem innerItem{get;set;} //many-to-one 

    } 
+0

這是一個已創建的數據庫,還是您有權更改數據模型? –

+0

它是一個已經創建的數據庫。但我可以在 – Diluu

+0

模型中進行更改請解釋id爲什麼只有id,它應該是物品id無處不在 –

回答

0

既然你有權更改數據模型,我建議如下:

ItemInnerItem表是相同的,除了表示父項的外鍵關係(InnerItem =>Item),如果Item缺少父項,則該關係將爲空。這將簡化Images並大大簡化這個和其他查詢向前移動。你的數據庫計劃是這樣的:

CREATE TABLE `Item` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` longtext, 
    `description` longtext, 
    `parentItemId` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `FK_Item_to_parent_itemId` FOREIGN KEY (`parentItemId`) REFERENCES `Item` (`id`) 
) 

CREATE TABLE `Images` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `url` longtext, 
    `itemId` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `FK_Images_Item_itemId` FOREIGN KEY (`itemId`) REFERENCES `Item` (`Id`), 
) 

這樣,你只需要一個約束,而且你不需要在邏輯代碼來處理,如果一個Image條目被用於內部圖像或正常圖片。如前所述,您需要做的只是確定parentItemId字段是否爲空。 更多此結構允許您將物品嵌套到比頂層更深的位置,如果您需要這樣做,則可以再嵌入更深的物品。

+0

奧普抱歉,我在這裏簡化了表格。但他們實際上有更多的領域。 – Diluu

+0

@Diluu好吧,夠公平的。我會嘗試調試你的代碼並更新我的答案。你可以發佈d.items使用的「items」和「innerItems」的C#定義嗎?我想看看Item和InnerItem之間的C#類關聯。 –

+0

謝謝@Nex Terrn,我用類定義更新了這個問題。 – Diluu