所以,几个小时以来,我一直在尝试在我的 WebAPI 模型中包含两个嵌套集合。经过数小时的反复试验、搜索、阅读和更多试验和错误后,我仍然无法弄清楚如何正确地做到这一点。
我使用 MySQL 作为数据库。
我的模型关系如下所示:
Account <-1:m-> Character
Character <-m:1-> Corporation
Corporation <-1:m-> CorporationWallet
Corporation <-1:m-> CorporationTaxes
模型是通过 EF Code-First 建立的,它在数据库中工作,导航关系也在工作。
我想要完成的是获取一个帐户及其角色,每个角色都应包括他们的公司以及该公司的钱包和税款。
以下工作并获取所需的一切,除了税:
Account account = db.Accounts
.Include(x => x.Characters.Select(y => y.Corporation.Wallets))
.Where(o => o.AccessToken == accessToken)
.Take(1)
.ToList()
.FirstOrDefault();
但是,当我希望包括税费时,它会抛出异常并且不会继续:
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();
失败并出现以下异常:
An error occurred while executing the command definition. See the inner exception for details.
内部异常:
<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>
如果我可以提供更多信息,请告诉我。
预先感谢您的帮助。
编辑 1:
下面给出了同样的异常和错误:
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()
最佳答案
正如 Janina 在评论中指出的那样,我的导航属性有些困惑。
我在用
[InverseProperty("Corporation")]
public List<CorporationTax> Taxes { get; set; }
[InverseProperty("Corporation")]
public List<CorporationWallet> Wallets { get; set; }
代替
[InverseProperty("Corporation")]
public virtual ICollection<CorporationTax> Taxes { get; set; }
[InverseProperty("Corporation")]
public virtual ICollection<CorporationWallet> Wallets { get; set; }
在我的模型层修复了所有导航集合属性后,我能够通过它获取我想要的所有内容
Account account = db.Accounts
.Where(o => o.AccessToken == accessToken)
.Take(1)
.ToList()
.FirstOrDefault();
关于c# - EF6 LINQ 嵌套包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025182/