繁体   English   中英

我如何 map SQL 服务器表的一列,以数字开头(列名称:2DBIT),到 ASP.NET MVC 项目(存储过程?

[英]How can I map a column of a SQL Server table, beginning with a number (name of the column:2DBIT), to an ASP.NET MVC project (stored procedure) CRUD?

我在 SQL 服务器中有一个表,其中包含一些列。 我写了一些存储过程。 我已经使用 Visual Studio 在 ASP.NET MVC 项目上加载了这些表和存储过程,并带有第一个数据库。 Visual Studio 为这个表生成了 crud,它与默认的实体框架一起工作。

但我的工作是使用存储过程来处理杂物。 我的问题开始了。

问题实际上是其中一列的名称开头有一个数字: 2DBIT (该作业不允许我更改列名)。

我调试代码并得到一个异常:

System.Data.Entity.Core.EntityCommandExecutionException: '数据读取器与指定的 'LotContext_NS.IS_MCC6CoaterLot' 不兼容。 类型的成员“C2DBit”在数据读取器中没有同名的对应列。

LotContext是我的上下文, IS_MCC6CoaterLot是表。

我认为问题在于,自动生成的代码将列上映射的IS_MCC6CoaterLot.cs的代码更改为: C2DBIT

编译器在数据库中找不到给定的列并抛出错误。

我能做些什么来解决这个问题? 更改列名是唯一的机会吗?

CRUD 与普通实体 FW 一起运行。 如果我用存储过程调整代码,我就会遇到这个问题。

//the mapped class of the table
public partial class IS_MCC6CoaterLot
{
  public Nullable<bool> C2DBit { get; set; }

}

//the action of the controller
public ActionResult Index()
{
    using (LotContext Db = new LotContext())
    {
        List<IS_MCC6CoaterLot> lots = Db.SP_getAllLots().ToList();
        return View(lots);
    }
}


View(Index):

@model IEnumerable<BASF.Models.SP_getAllLots_Result>
...
@Html.DisplayFor(modelItem => item.C2DBit)



Column-name of the table: "2DBIT"

当我调试代码并得到异常时:

System.Data.Entity.Core.EntityCommandExecutionException: 'The data reader is incompatible with the specified 'LotContext_NS.IS_MCC6CoaterLot'. A member of the type, 'C2DBit', does not have a corresponding column in the data reader with the same name.'

我认为问题在于,自动生成的代码将列上映射的 IS_MCC6CoaterLot.cs 的代码更改为:C2DBIT。 编译器在数据库中找不到给定的列并抛出错误。 但我能做些什么来解决这个问题? 更改列名是唯一的机会吗?

您可能遇到的问题是您的实体没有更新以反映对数据库的更改。

以下是一个相当不错的参考: https://www.entityframeworktutorial.net/efcore/working-with-stored-procedure-in-ef-core.aspx

暂无
暂无

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

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