简体   繁体   English

MVC Linq到实体中的关系数据库问题

[英]Problem with Relational Database in MVC Linq to Entities

I was hoping someone could tell me what is wrong here. 我希望有人能告诉我这里出了什么问题。 I have three tables, LU_LOC_Suburb, Listings, ListingMessages. 我有三个表LU_LOC_Suburb,清单,ListingMessages。


Listings has the following columns 清单包含以下各列

ID ID

SuburbID (Coming from LU_LOC_Suburb) SuburbID(来自LU_LOC_Suburb)

more... 更多...


ListingMessages has the following columns ListingMessages具有以下列

ID ID

ListingID (Coming from Listings) ListingID(来自清单)

more... 更多...


I'm trying to create a Messages page where I first get all the Message for a User; 我正在尝试创建一个“消息”页面,在该页面中我首先获取一个用户的所有消息;

IQueryable<ListingMessage> Messages = from x in DBEntities.ListingMessageSet.Include("Listings")
                                      where x.Listings.Users.ID == UserID
                                      select x;

I then send this to a View, lets call it Messages for User. 然后,我将其发送到视图,将其称为“用户消息”。

I also limit the messages by listing by taking all the messages for a user and then only selecting the ones that are related to a particular listing; 我还通过列出一条消息来限制消息,方法是为用户获取所有消息,然后仅选择与特定列表相关的消息。

Messages = from x in Messages
           where x.Listings.ID == ListingID
           select x;

I then send this to a View, lets call it Messages for Listing. 然后,我将其发送到视图,将其称为“ 消息列表”。

In my View Pages, I want to write the suburb name to screen which I can do by; 在我的“查看页面”中,我想将郊区名称写到屏幕上,我可以这样做。

<%= Html.Encode(item.Listings.LU_LOC_Suburb.Name) %>

Now here's the problem... 现在这是问题所在...

This gives the error - Object reference not set to an instance of an object - when I DON'T limit the Messages by Listing (in the Messages for User View). 当我不通过列表限制消息时(在用户视图的消息中 ),这会产生错误-对象引用未设置为对象的实例。 I do not get this error when I limit the messages by listing. 当我通过列表限制消息时,我没有收到此错误。

I dont understand why this is happening. 我不明白为什么会这样。 I know it's something simple and am hoping you guys can help me resolve this? 我知道这很简单,希望你们能帮助我解决这个问题?

Thanks in advance, 提前致谢,

Sheefy ef

可能是您没有在邮件查询中“包含”“列表”吗?

As you may or may not know, IQueryable is a 'lazy' representation of the actual query. 您可能知道也可能不知道, IQueryable是实际查询的“惰性”表示。 It is not executed until enumerated over. 直到枚举结束才执行。 From your code sample, it seems like you're sending the IQueryable directly to your view. 从您的代码示例来看,您似乎正在将IQueryable直接发送到视图。 Not sure if that is 'wrong' but I always make sure the query is actually executed in the controller. 不确定这是否是“错误”,但我始终确保查询实际上是在控制器中执行的。 You can do this by adding a ToList() to the statement, when adding it to your ViewModel (or ViewData). 您可以通过将ToList()添加到语句中,然后将其添加到ViewModel(或ViewData)中来实现。

I'm not saying that this will solve your problem, but it will make debugging much easier. 我不是说,这将解决您的问题,但它会使调试更加容易。

Just incase anyone stumbles past this post and is wondering how to overcome the problem, I found the solution here . 万一有人跌跌撞撞地走过这个帖子,想知道如何解决这个问题,我在这里找到了解决方案。

By using the .Include("RelatedTable.ParentTable") I've been able to get around the problem eventhough I still don't quite understand why it was not bringing in the related tables when I wasn't narrowing it down by ListingID. 通过使用.Include(“ RelatedTable.ParentTable”),即使我仍然不十分了解为什么我没有通过ListingID缩小范围时为什么没有引入相关表,我仍然能够解决该问题。

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

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