ASP.NET MVC 5 / C# Foreach 循环 - 重复相同的数据

发布于 2025-01-16 21:54:56 字数 2455 浏览 0 评论 0原文

这看起来很“初学者”,但我正在挣扎。

我有一个连接两个表(EmployeeCars)的 SQL Server 视图。我有一个该视图的模型。

当我打开 SQL Server Management Studio 并使用与员工 A 匹配的 Select * 记录查询 SQL Server 视图时。我得到两条记录,因为该员工在 Employee 中列出了一次表中,并在 Cars 表中出现两次。在 SQL Server Management Studio 中,我验证了该人的两辆汽车是唯一的。正是如此。

但我一定在 ASP.NET MVC 5 应用程序中的代码中做错了什么,因为当我加载 Index 视图时,应该显示两个唯一的记录 - 相反,它显示了完全相同的记录记录(同一辆车)两次。

在我的控制器中:

public ActionResult Index()
{
    string MyName = 
    System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    String result = MyName.Substring(MyName.Length - 6);
    ViewBag.WhoAmI = result;

    UserPrincipal userPrincipal = UserPrincipal.Current;
    String name = userPrincipal.DisplayName;
    ViewBag.MyRealID = name;

    try
    {
        Web_Parkinglot_Header_FullViewRepository oItem = new 
        Web_Parkinglot_Header_FullViewRepository();
        Var item = oItem.GetbyLogon((string)result);
       
        if (!String.IsNullOrEmpty(result))
        {
        }                              

        return View(item.ToList());
    }

    catch (Exception ex)
    {
        throw ex;
    }
}

存储库:

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

视图:

foreach (var item in Model)
{
    <table class="table">
        <tr>
            <td>
                <img src="~/Content/images/bmw.png" width="75" height="50" />
            </td>
            <td>
                <b>Vehicle Make:</b> @Html.DisplayFor(modelItem => item.veh_make)
            </td>
            <td>
                <b>Model:</b> @Html.DisplayFor(modelItem => item.veh_model)
            </td>

            <td>
                <b>Color:</b> @Html.DisplayFor(modelItem => item.veh_color)
            </td>
            <td>
                <b>Plate:</b> @Html.DisplayFor(modelItem => item.veh_plate)
            </td>
        </tr>
    </table>
}

我不明白的是:为什么当我直接查询 SQL Server 视图时,我得到了 logon_id = A 的正确 2 辆车。但是当我通过存储库运行相同的查询时,我得到 2 条记录,但在 foreach 循环中两次是同一条记录。

SQL Server 视图是内连接。我还可以发布更多信息(型号等,如果需要的话)

This seems pretty 'beginner' but I am struggling.

I have a SQL Server view that joins two tables (Employee and Cars). I have a model for that view.

When I open SQL Server Management Studio and query the SQL Server view with Select * records that match employee A. I get two records back, because the employee is listed once in Employee table, and twice in the Cars table. In SQL Server Management Studio, I verify the two Cars for said person, are unique. Exactly as it should be.

But I must be doing something wrong in my code, within the ASP.NET MVC 5 app because when I load the Index view, that should display the two unique records - it, instead shows me the exact same record (same car) twice.

In my controller:

public ActionResult Index()
{
    string MyName = 
    System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    String result = MyName.Substring(MyName.Length - 6);
    ViewBag.WhoAmI = result;

    UserPrincipal userPrincipal = UserPrincipal.Current;
    String name = userPrincipal.DisplayName;
    ViewBag.MyRealID = name;

    try
    {
        Web_Parkinglot_Header_FullViewRepository oItem = new 
        Web_Parkinglot_Header_FullViewRepository();
        Var item = oItem.GetbyLogon((string)result);
       
        if (!String.IsNullOrEmpty(result))
        {
        }                              

        return View(item.ToList());
    }

    catch (Exception ex)
    {
        throw ex;
    }
}

Repository:

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

View:

foreach (var item in Model)
{
    <table class="table">
        <tr>
            <td>
                <img src="~/Content/images/bmw.png" width="75" height="50" />
            </td>
            <td>
                <b>Vehicle Make:</b> @Html.DisplayFor(modelItem => item.veh_make)
            </td>
            <td>
                <b>Model:</b> @Html.DisplayFor(modelItem => item.veh_model)
            </td>

            <td>
                <b>Color:</b> @Html.DisplayFor(modelItem => item.veh_color)
            </td>
            <td>
                <b>Plate:</b> @Html.DisplayFor(modelItem => item.veh_plate)
            </td>
        </tr>
    </table>
}

What I don't understand is: why, when I query the SQL Server view directly, I get the correct 2 vehicles for logon_id = A. But when I run the same query via the repository, I get 2 records, but it's the same record twice, in the foreach loop.

The SQL Server view is an inner join. I can also post more info (Model, etc if needed)

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

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

发布评论

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

评论(1

北方的巷 2025-01-23 21:54:56

从实体框架使用视图时,存在一个微妙的问题。

如果您有一个表,请将其与 EF 一起使用,您需要有一个主键来唯一标识每一行。通常,这是一个单列,例如 ID 或类似的内容。

对于视图,您没有“主键”的概念 - 视图仅包含某些表中的某些列。

因此,当 EF 映射视图时,它无法找到主键 - 因此,它将使用视图中的所有不可空列作为“替代”主键。

我不知道您的情况是什么 - 您应该能够从 .edmx 模型中看出。

所以问题实际上是视图上不能有显式的主键。

最简单的解决方案是简单地包含视图定义中涉及的两个表中的主键(即使您不想显示它们)。

这样,这些将出现在视图中,并且它们将不可为空,因此您应该没问题并且不会得到任何重复项。

There is a subtle problem with views when used from Entity Framework.

If you have a table, do use it with EF, you need to have a primary key to uniquely identify each row. Typically, that's a single column, e.g. an ID or something like that.

With a view, you don't have the concept of a "primary key" - the view just contains some columns from some tables.

So when EF maps a view, it cannot find a primary key - and therefore, it will use all non-nullable columns from the view as "substitute" primary key.

I don't know what these are in your case - you should be able to tell from the .edmx model.

So the problem really is that you can't have explicit primary keys on a view.

The easiest solution is to just simply include the primary key from both tables involved in your view definition (even if you don't want to show them ever).

That way, these will be in the view, and they will be non-nullable, and thus you should be fine and not getting any duplicates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文