C#LINQ查询外键循环

时间:2016-03-09 17:02:41

标签: c# mysql linq

在我的MySQL数据库中,我有三个表,其中Items和innerItem都有图像。因此,在图像表行中,innerItem_id或itemId不为空,但不是两者。

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 ();

当排除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'

对此有什么想法吗?

更新

数据模型类

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

    }

1 个答案:

答案 0 :(得分:0)

由于您有权更改数据模型,因此我建议如下:

您的ItemInnerItem表是相同的,但外键关系(InnerItem =&gt; 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字段是否为空。 进一步此结构允许您嵌套比顶层更深的项目,如果您需要,则可以更深层次。