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

查看:462
本文介绍了使用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; }
}

DogTable

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 p>

and the mapping table: 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; }
}

现在,我试图加入这三个表,并获得OwnerName和DogName,当通过一个OwnerId。这是我的查询:

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。有关其他支持的类型,请参阅Microsoft
.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将模型返回到BusinessLayer,其中使用AutoMapper完成DTO映射。在EF生成的模型中没有任何DataContracts。此外,到目前为止,在项目中,我已经远离DTO直接从DataLayer。

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.

如果我使用的Lambda表达式类似于实体框架加入3表

If I use Lambda expression which is similar to the one mentioned on 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天全站免登陆