如何将 SQL Server 表的列(以数字(列的名称:2DBIT)开头)映射到 ASP.NET MVC 项目(存储过程



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

但我的工作是使用crud的存储过程。在那里,我的问题开始了。

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

我调试代码并得到异常:

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

LotContext是我的背景,IS_MCC6CoaterLot是表格。

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

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

我该怎么做才能解决这个问题?更改列名称是唯一的机会吗?

CRUD 正在使用普通实体固件运行。如果我使用存储过程调整代码,我会遇到这个问题。

//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.'

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

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

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

最新更新