使用 Lambda 在 Entity Framework 6 中加入多个表后获取选定的值 [英] Get selected values after joining multiple tables in Entity Framework 6 using Lambda

查看:16
本文介绍了使用 Lambda 在 Entity Framework 6 中加入多个表后获取选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表(这个问题的简化示例):

使用 EntityFramework Database-First 方法生成模型.

Models are generated using EntityFramework Database-First approach.

OwnerModel

public partial class Owner
{
    public Owner()
    {            
       this.OwnerDogMapper= new HashSet<OwnerDogMapper>();
    }
    public string OwnerId { get; set; }
    public string OwnerName { get; set; }
    public virtual ICollection<OwnerDogMapper> OwnerDogMapper{ get; set; }
}

狗桌

public partial class Dog
{
    public Dog()
    {
        this.OwnerDogMapper= new HashSet<OwnerDogMapper>();
    }
    public string DogId { get; set; }
    public string DogName { get; set; }
    public virtual ICollection<OwnerDogMapper> OwnerDogMapper{ get; set; }
}

和映射表:OwnerDogMapper

public partial class OwnerDogMapper
{
    public string OwnerId { get; set; }
    public string DogId { get; set; }
    public virtual Owner Owner { get; set; }
    public virtual Dog Dog { get; set; }
}

现在,我正在尝试加入这三个表,并在传递 OwnerId 时获取 OwnerName 和 DogName.这是我的查询:

Now, I'm trying to join these three tables and get the OwnerName and DogName when an OwnerId is passed. Here is my query:

var output = await (from o in db.Owner
                    join odm in db.OwnerDogMapper on o.OwnerId equals odm.OwnerId
                    join d in db.Dog on odm.DogId equals d.DogId
                    where o.OwnerId == '01'
                    select new { o.OwnerName, d.DogName }
                   ).ToListAsync();

但它会抛出异常:

例外:ObjectContent`1"类型无法序列化内容类型application/xml"的响应正文;字符集=utf-8'.

Exception: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

类型'<>f__AnonymousType2`2[System.String,System.String]' 不能序列化.考虑用 DataContractAttribute 标记它属性,并标记您想要序列化的所有成员DataMemberAttribute 属性.如果类型是集合,请考虑用 CollectionDataContractAttribute 标记它.见微软其他受支持类型的 .NET Framework 文档.

Type '<>f__AnonymousType2`2[System.String,System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

DataLayer 将模型返回到使用 AutoMapper 完成 DTO 映射的 BusinessLayer.EF 生成的模型中没有任何 DataContracts.此外,到目前为止,在项目中,我一直远离直接从 DataLayer 传递 DTO.

The DataLayer returns Models to the BusinessLayer where the DTO mapping is done using AutoMapper. There aren't any DataContracts in the Models generated by the EF. Also, so far in the project, I have stayed away from passing DTOs directly from the DataLayer.

如果我使用类似于 Entity Framework Join 3 Tables

var output = await db.Owner
                   .Where(o => o.OwnerId == "01")
                   .Include(odm => odm.OwnerDogMapper.Select(d => d.Dog))
                   .ToListAsync();

但是,就我而言,[Owner] 和 [Dog] 表之间没有任何关系.使用这个lamba查询,它进入一个无限循环,我得到一个StackOverflowException":D -

However, in my case I don't have any relationship between the [Owner] and [Dog] Table. With this lamba query, it goes to an infinite loop and I get an "StackOverflowException" :D -

"确保没有无限循环或无限递归

"make sure you do not have an infinite loop or infinite recursion

表映射或生成的模型是否存在根本问题?还是我的查询不对?

Is there something fundamentally wrong with the table mapping or the models generated? Or my query is not right?

推荐答案

我后来发现了这个问题.我在 WebAPIconfig.cs 文件中更改了 configFormatter:

I had later figured out the issue. I had the changed the configFormatter in my WebAPIconfig.cs file:

  config.Formatters.Remove(config.Formatters.XmlFormatter);

删除它后,LINQ 查询按预期工作.我希望它将来可以帮助其他人!

Once I removed it, the LINQ query worked as expected. I hope it may help someone else in future!

这篇关于使用 Lambda 在 Entity Framework 6 中加入多个表后获取选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆