实体框架:数据库更新后 LINQ Include() 不起作用,为什么?
我是实体框架和 LINQ 的新手,遇到了一个相当奇怪的场景。
我一直在使用以下查询来返回帐户信息:
var account = ((from acct in _entities.Account
join m in _entities.Item on acct.Id equals m.Account.Id
where acct.Id == accountId && m.ItemNumber.EndsWith(itemNumber)
select acct) as ObjectQuery<Account>).Include("Item.ItemDetails");
我们最近对数据库进行了一些更改并生成了一个新的 edmx 文件。更改后,上述查询仍返回帐户和关联的项目,但不再包含 ItemDetails。
我已经验证了查询返回的 SQL,并且似乎没有任何错误,因为返回了正确的数据。
此外,我在 edmx 文件中的 Item 和 ItemDetails 对象之间没有看到任何不同,因为这些对象没有更改并且导航属性在那里。
有人见过这个吗?
谢谢
I am new to Entity Framework and LINQ and have run into a rather odd scenario.
I have been using the following query to return account information:
var account = ((from acct in _entities.Account
join m in _entities.Item on acct.Id equals m.Account.Id
where acct.Id == accountId && m.ItemNumber.EndsWith(itemNumber)
select acct) as ObjectQuery<Account>).Include("Item.ItemDetails");
We recently made some changes to the database and generated a new edmx file. Following the change the above query still returns the account and associated Item but the ItemDetails is no longer being included.
I have validated the SQL returned by the query and there doesn't seem to be anything wrong as the correct data is being returned.
Furthermore I don't see anthing different in the edmx file between the Item and ItemDetails objects as these were not changed and the navigation property is there.
Has anyone seen this before?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Include(...) 中使用导航属性的名称,因此最好从 .edmx 中检查属性的确切名称(特别是单数或复数时)。
您也可以尝试像这样更改查询:
In Include(...) is used the name of the navigation property so it will be good to check the exact name of the property from the .edmx (especially if it is singular or plural).
Also you can try to change the query like this:
您有两种可能的情况之一:
Item
与Account
存在关系(在实体模型中表示为 EntityAssociation,在数据库中表示为外键):< /p>< code>Item 设置和
Account
设置,因此,您必须像您所做的那样在 LINQ 中指定联接。情况 1:如果是这种情况,那么您不需要 join 语句...通过选择
Acount.Item
自然会为您提供Item.AccountID
所在的所有项目等于Account.ID
因此您的连接语句:
join m in _entities.Item on acct.Id equals m.Account.Id
基本上告诉
Item
循环回Account
以检查 ID。如果尚未连接,则无法获取m.Account.ID
情况 2:如果
Account
和之间没有关系Item
,那么.Include()
肯定不起作用,因为您的模型中不存在导航属性。结论:检查您的新模型,看看
Account
和Item
之间是否存在关系。如果是,则删除连接。如果没有关系,那么你就做错了。以下是假设场景 1 且
Account.Item
不是集合的 select 语句:You have one of two possible scenarios:
Item
has a relationship toAccount
(expressed in your Entity Model as a EntityAssociation and in DB as a foreign key):There is no relationship between
Item
set andAccount
set, hence, you must specify a join in LINQ as you have done.Case 1: if this is the case, then you don't need a join statement... by selecting
Acount.Item
will naturally give you all items whereItem.AccountID
is equal toAccount.ID
So your join statement:
join m in _entities.Item on acct.Id equals m.Account.Id
has basically told
Item
to loop back ontoAccount
to check the ID. If they were not already connected, then you could not have gottenm.Account.ID
Case 2: If there is no relationship between
Account
andItem
, then the.Include()
will definitely not work because the navigational property DOES NOT exist in your model.Conclusion: Check your new model to see if a relationship exists between
Account
andItem
. If yes, then remove the Join. If no relationship, then you've done something wrong.Here is a select statement assuming scenario 1 and that
Account.Item
is not a collection: