无法获得预期的结果(模型中的VAR项目)

发布于 2025-02-06 05:22:55 字数 1947 浏览 5 评论 0原文

我有MVC5/C#索引视图,该视图将显示所有员工注册车辆的停车场。

foreach (var item in Model)
{
<table class="table">
<tr>
<td>                    
<b>Vehicle Make:</b> @Html.DisplayFor(modelItem => item.veh_make)
</td>
<td>
<b>Model:</b> @Html.DisplayFor(modelItem => item.veh_model)
</td>
</tr>
</table>
}

该模型基于连接两个SQL表的视图。标头(表1)和车辆(表2)。

SQL视图是左外连接,在车辆表(许多)中,与与该用户相关的所有车辆的用户名/标头(1)创造了结果。

我的车桌里有两辆车。当我打开logon_id = {我的ID}的SQL视图时,它在结果中显示了两个记录。我看到我的宝马和斯巴鲁。因此,我相信我的SQL加入是正确的吗?

但是,当我从控制器中复制此内容时,我在MVC索引视图上没有得到预期的结果(2种不同的车辆)。相反,我看到同一辆车列出了两次。

我正在从控制器中击中视图:

public ActionResult Index(string id)
{
string MyName  = System.Web.HttpContext.Current.User.Identity.Name;
id = ParseMe.ParseLogin(MyName);
Web_Parkinglot_Header_FullViewRepository oItem = new Web_Parkinglot_Header_FullViewRepository();
var item = oItem.GetbyLogon((string)id);
}

getBylogon

public List<Web_Parkinglot_Header_FullView> GetbyLogon(string id)
{
return this.Context.Web_Parkinglot_Header_FullViews.Where(a => a.logon_id == id).ToList();
}

添加我的SQL JON加入,以防万一我在这里缺少某些内容:

SELECT dbo.Header.last_name, dbo.Header.logon_id, dbo.Vehicles.veh_make, dbo.Vehicles.veh_model
FROM dbo.Header LEFT OUTER JOIN
dbo.Vehicles ON dbo.Header.logon_id = dbo.Vehicles.logon_id

当我在SQL Server Mgmt Studio中手动运行此内容时(其中logon_id = m y ID ),我看到两排都与我的两辆车看到。

通过我的型号代码运行此操作,我两次获得同一辆车。

我在故障排除中迷路的地方是我的SQL视图将两辆车弹回去,其中logon_id =我的ID。因此,为什么我的GetByLogon将同一辆车带回了两次,而不是两辆不同的车辆。我在代码中不了解什么?

解决方案:

return this.Context.Web_Parkinglot_Header_FullViews.AsNoTracking().Where(a => a.logon_id == id).ToList();

感谢TopSail将我转向正确的方向,并带有引用Asnotracking()的链接。

这是我问题的决心,现在我看到了回报的预期结果。事实:尽管它有效,但我不完全了解Asnotracking(),因此我仍然有一些研究要做。

I have an MVC5/C# Index View that will display all of an employees registered vehicles for the parking lot.

foreach (var item in Model)
{
<table class="table">
<tr>
<td>                    
<b>Vehicle Make:</b> @Html.DisplayFor(modelItem => item.veh_make)
</td>
<td>
<b>Model:</b> @Html.DisplayFor(modelItem => item.veh_model)
</td>
</tr>
</table>
}

The Model is based off of a View that joins two SQL Tables. Header (Table 1) and Vehicles (Table2).

The SQL View is a Left Outer Join, creating a result of the user Name/Header (1) to all of the vehicles associated to that user, in the Vehicle Table (Many).

I have two cars in the Vehicle Table. When I Open the SQL View Where logon_id = {my id}, It shows two records in the results. I see my BMW and Subaru. And so, I believe that my SQL Join is correct?

But, when I duplicate this from my Controller, I don't get the expected results (2 different vehicles) on my MVC Index View. Instead, I see the same vehicle listed twice.

I am hitting the View, from my controller:

public ActionResult Index(string id)
{
string MyName  = System.Web.HttpContext.Current.User.Identity.Name;
id = ParseMe.ParseLogin(MyName);
Web_Parkinglot_Header_FullViewRepository oItem = new Web_Parkinglot_Header_FullViewRepository();
var item = oItem.GetbyLogon((string)id);
}

And The GetByLogon

public List<Web_Parkinglot_Header_FullView> GetbyLogon(string id)
{
return this.Context.Web_Parkinglot_Header_FullViews.Where(a => a.logon_id == id).ToList();
}

Adding my SQL Join, just in case I am missing something here:

SELECT dbo.Header.last_name, dbo.Header.logon_id, dbo.Vehicles.veh_make, dbo.Vehicles.veh_model
FROM dbo.Header LEFT OUTER JOIN
dbo.Vehicles ON dbo.Header.logon_id = dbo.Vehicles.logon_id

When I manually run this in SQL Server Mgmt Studio, (where logon_id = my Id), I see two rows with both of my individual cars.

Running this via my model code, I get the same vehicle twice.

Where I get lost in troubleshooting is that my SQL VIEW brings back both vehicles Where logon_id = my id. So, why does my GetByLogon bring back the same vehicle twice, instead of two different vehicles. What am I not understanding in my code?

Resolution:

return this.Context.Web_Parkinglot_Header_FullViews.AsNoTracking().Where(a => a.logon_id == id).ToList();

Thank you to TopSail for steering me in the right direction, with a link that referenced AsNoTracking().

This was the resolve to my issue and I am now seeing the expected results in my return. Truth: Although it works, I don't fully understand AsNoTracking(), and so I will still have some research to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文