简体   繁体   中英

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?

I have a table in SQL Server with some columns. I wrote some stored procedures. I have loaded those tables and stored procedures on an ASP.NET MVC project with Visual Studio, with first database. Visual Studio produced the crud for this table, that is working with the default Entity Framework.

But my job is to use stored procedures for the crud. There my problems began.

The problem really is that one of the columns has a number at the beginning of its name: 2DBIT (the job doesn't allow me to change the column name).

I debug the code and get an exception:

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

LotContext is my context, and IS_MCC6CoaterLot is the table.

I think the problem ist, that the automatically generated code changes the code of the mapped IS_MCC6CoaterLot.cs on the column with: C2DBIT .

The compiler doesn't find the given column in the database and throws the error.

What can I do to solve this problem? Is changing the column name the only opportunity?

The CRUD is running with normal entity FW. If I adjust the code with the stored procedures, I have that problem.

//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"

When I debug the code and get an exception:

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

I think the problem ist, that the automatically generated code change the code of the mapped IS_MCC6CoaterLot.cs on the column with: C2DBIT. The Compiler don´t find the given column in the database und throw the error. But what can i do to solve this problem? is changing the column-name the only opportunity?

The problem you maybe running into is that your entities aren't updated to reflect changes to your database.

The following is a pretty decent reference: https://www.entityframeworktutorial.net/efcore/working-with-stored-procedure-in-ef-core.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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