简体   繁体   English

EF6 LINQ嵌套包括

[英]EF6 LINQ nested includes

So, I've been stuck for hours trying to include two nested collections in my model for a WebAPI. 所以,我已经被困了几个小时试图在我的模型中包含两个嵌套集合用于WebAPI。 After hours of trial and error, searching, reading and more trial and error I still cant figure out how to do this properly. 经过几个小时的试验和错误,搜索,阅读和更多的反复试验,我仍然无法弄清楚如何正确地做到这一点。

Im using MySQL as database. 我使用MySQL作为数据库。

My model relationships looks like this: 我的模型关系看起来像这样:

Account <-1:m-> Character
Character <-m:1-> Corporation
Corporation <-1:m-> CorporationWallet
Corporation <-1:m-> CorporationTaxes

The model is set up through EF Code-First and it's working in the database, the navigation relationships are working as well. 该模型通过EF Code-First设置,它在数据库中工作,导航关系也正常。

What I'm trying to accomplish is to fetch an account and it's characters, each character should include their corporation and that corporations wallets and taxes. 我想要完成的是获取一个帐户及其角色,每个角色应该包括他们的公司以及公司的钱包和税收。

The following works and fetches everything needed, except the taxes: 以下工作和提取所需的一切,除税:

Account account = db.Accounts
    .Include(x => x.Characters.Select(y => y.Corporation.Wallets))
    .Where(o => o.AccessToken == accessToken)
    .Take(1)
    .ToList()
    .FirstOrDefault();

However when i wish to include the taxes it throws an exception and doesn't continue: 但是,当我希望包含税收时,它会引发异常并且不会继续:

Account account = db.Accounts
    .Include(x => x.Characters.Select(y => y.Corporation.Wallets))
    .Include(x => x.Characters.Select(y => y.Corporation.Taxes))
    .Where(o => o.AccessToken == accessToken)
    .Take(1)
    .ToList()
    .FirstOrDefault();

It fails with the following exception: 它失败,出现以下异常:

An error occurred while executing the command definition. See the inner exception for details.

Inner exception: 内部异常:

<Message>An error has occurred.</Message>
<ExceptionMessage>Unknown column 'UnionAll1.C1' in 'field list'</ExceptionMessage>
<ExceptionType>MySql.Data.MySqlClient.MySqlException</ExceptionType>
<StackTrace>
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
</StackTrace>

Let me know if there is any more information i can provide. 如果我能提供更多信息,请告诉我。

Thanks in advance for your help. 在此先感谢您的帮助。

Edit 1: 编辑1:

The following gives the same exception and error: 以下内容给出了相同的异常和错误:

var query = from acc in db.Accounts
            where acc.AccessToken == accessToken
            select new
            {
                acc,
                Characters = from cha in acc.Characters
                             select new
                             {
                                 cha,
                                 Account = cha.Account,
                                 Corporation = cha.Corporation,
                                 Taxes = from tax in cha.Corporation.Taxes
                                         select new
                                         {
                                             tax,
                                             Corporation = tax.Corporation
                                         },
                                 Wallets = from wallet in cha.Corporation.Wallets
                                           select new
                                           {
                                               wallet,
                                               Corporation = wallet.Corporation
                                           }
                             }
            };
Account account = query
    .Select(x => x.acc)
    .FirstOrDefault()

As Janina pointed out in a comment, my navigational properties were jumbled a bit. 正如贾尼娜在评论中指出的那样,我的导航属性有些混乱。

I was using 我在用

[InverseProperty("Corporation")]
public List<CorporationTax> Taxes { get; set; }
[InverseProperty("Corporation")]
public List<CorporationWallet> Wallets { get; set; }

instead of 代替

[InverseProperty("Corporation")]
public virtual ICollection<CorporationTax> Taxes { get; set; }
[InverseProperty("Corporation")]
public virtual ICollection<CorporationWallet> Wallets { get; set; }

After fixing all the navigational collection properties across my model layer i was able to fetch all i wanted through 在我的模型层修复所有导航集合属性后,我能够获取我想要的所有内容

Account account = db.Accounts
    .Where(o => o.AccessToken == accessToken)
    .Take(1)
    .ToList()
    .FirstOrDefault();

Use below 使用下面

Account account = db.Accounts
    .Include(x => x.Characters)
    .Where(o => o.AccessToken == accessToken)
    .Take(1)
    .ToList()
    .FirstOrDefault();

Because Taxes and Wallets are in the same Navigation Property so you don't need to Include agian 由于Taxes和Wallets属于同一导航属性,因此您不需要包含agian

Then you can get every property from Character Model 然后你可以从角色模型获得每个属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM