You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC调用ViewModel报错:SqlException无效对象名OfficerLangVM

问题分析与解决

错误根源

你遇到的SqlException: Invalid object name 'OfficerLangVM',核心原因是把视图模型(ViewModel)当成了Entity Framework的实体类直接查询。OfficerLangVM是用来封装视图所需数据的定制模型,并非数据库中存在的表,EF Core会将_Db.OfficerLangVMs解析为数据库表,但实际不存在这张表,因此触发报错。

解决步骤

1. 修正Controller的数据查询逻辑

需要从数据库中实际存在的实体表(比如Officers、Agencies等,根据你的业务实体调整)查询数据,再将实体数据投影映射到OfficerLangVM,而非直接查询ViewModel。

假设你的DbContext中有Officers和Agencies两个实体DbSet,修改后的Controller代码示例如下:

public IActionResult Privacy(string AgencyCity, string AgName, string AgGuid)
{
    if (_Db.Officers == null)
    {
        return Problem("Entity set 'FLIPV2Context.Officers' is null.");
    }

    // 从实体表查询,投影映射到OfficerLangVM
    var officersQuery = from o in _Db.Officers
                        join a in _Db.Agencies on o.AgencyId equals a.Id // 假设Officer与Agency存在关联
                        select new OfficerLangVM
                        {
                            OfficerKey = o.Id.ToString(), // 映射Officer主键
                            AgencyName = a.Name, // 从Agency表获取机构名称
                            City = a.City, // 从Agency表获取城市
                            FirstName = o.FirstName,
                            LastName = o.LastName,
                            // 其他字段根据实体关系补充映射
                        };

    // 应用筛选条件
    if (!String.IsNullOrEmpty(AgencyCity))
    {
        officersQuery = officersQuery.Where(vm => vm.City!.Contains(AgencyCity));
    }
    if (!String.IsNullOrEmpty(AgName))
    {
        officersQuery = officersQuery.Where(vm => vm.AgencyName!.Contains(AgName));
    }

    // 执行查询并返回视图
    var officersVM = officersQuery.ToList();
    return View(officersVM);
}

2. 明确ViewModel的定位

OfficerLangVM的作用是聚合视图需要的所有数据,它不需要和数据库表一一对应,也不需要在DbContext中注册为DbSet。你需要根据业务逻辑,从各个实体表提取数据,组装成ViewModel实例。

3. 修复视图中的小问题

你的视图代码存在两处小错误:

  • table-boardered拼写错误,应为table-bordered
  • 多余的外层<tbody>标签,导致HTML结构无效

修正后的视图表格部分代码:

<br />
<table class="table table-bordered table-striped" style="margin-left:-250px; background-color:lightgrey; width:100%" cellpadding="5">
    <thead>
        <tr>
            <th>@Html.DisplayNameFor(model => model.AgencyName)</th>
            <th>@Html.DisplayNameFor(model => model.City)</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td> @Html.DisplayFor(modelItem => item.AgencyName)</td>
                <td>@Html.DisplayFor(modelItem => item.City)</td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.OfficerKey">Edit Officers</a>
                </td>
            </tr>
        }
    </tbody>
</table>

关键注意点

  • 视图模型(ViewModel)≠ 实体模型(Entity):实体模型对应数据库表,ViewModel是为视图定制的数据容器
  • 必须通过实体查询后投影到ViewModel,不能直接用ViewModel作为EF查询的数据源

内容的提问来源于stack exchange,提问作者cg1323

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 18:23:14