实体框架:数据库更新后 LINQ Include() 不起作用,为什么?

发布于 2024-08-19 01:21:17 字数 641 浏览 9 评论 0原文

我是实体框架和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-08-26 01:21:17

在 Include(...) 中使用导航属性的名称,因此最好从 .edmx 中检查属性的确切名称(特别是单数或复数时)。

您也可以尝试像这样更改查询:

var account = from acct in _entities.Account.Include("Item.ItemDetails")
              join m in _entities.Item 
                  on acct.Id equals m.Account.Id
              where acct.Id == accountId && m.ItemNumber.EndsWith(itemNumber)
              select acct;

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:

var account = from acct in _entities.Account.Include("Item.ItemDetails")
              join m in _entities.Item 
                  on acct.Id equals m.Account.Id
              where acct.Id == accountId && m.ItemNumber.EndsWith(itemNumber)
              select acct;
小猫一只 2024-08-26 01:21:17

您有两种可能的情况之一:

  1. ItemAccount 存在关系(在实体模型中表示为 EntityAssociation,在数据库中表示为外键):< /p>

  2. < 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() 肯定不起作用,因为您的模型中不存在导航属性。

结论:检查您的新模型,看看 AccountItem 之间是否存在关系。如果是,则删除连接。如果没有关系,那么你就做错了。

以下是假设场景 1 且 Account.Item 不是集合的 select 语句:

var account = from acct in _entities.Account.Include("Item.ItemDetails")
              where acct.Id == accountId && acct.Item.ItemNumber.EndsWith(itemNumber)
              select acct;

You have one of two possible scenarios:

  1. Item has a relationship to Account (expressed in your Entity Model as a EntityAssociation and in DB as a foreign key):

  2. There is no relationship between Item set and Account 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 where Item.AccountID is equal to Account.ID

So your join statement: join m in _entities.Item on acct.Id equals m.Account.Id
has basically told Item to loop back onto Account to check the ID. If they were not already connected, then you could not have gotten m.Account.ID

Case 2: If there is no relationship between Account and Item, 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 and Item. 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:

var account = from acct in _entities.Account.Include("Item.ItemDetails")
              where acct.Id == accountId && acct.Item.ItemNumber.EndsWith(itemNumber)
              select acct;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文