如何在ADO.NET实体框架中无错误地调用存储过程?

时间:2022-04-25 02:21:12

How do I call a stored procedure without error in ADO.NET Entity Framework? If I use the code below, I get an error:

如何在ADO.NET实体框架中无错误地调用存储过程?如果我使用下面的代码,我会收到一个错误:

adminNameContext.AddItemCategory(12, "ggf", DateTime.Now);  

Error:

The data reader is incompatible with the specified 'NetTanitimTestModel.Categories'. A member of the type, 'ID', does not have a corresponding column in the data reader with the same name.

数据读取器与指定的'NetTanitimTestModel.Categories'不兼容。类型为“ID”的成员在数据读取器中没有具有相同名称的相应列。

ALTER procedure [dbo].[sp_AddItemCategory]  
(  
  @item int,   
  @category nvarchar(50),  
  @date smalldatetime   
)  
as  
begin  
  if(@item=-1)  
  begin  
    insert into Categories(PARENTID,Category,Date) values(null,@category,@date)  
  end  
  else  
  begin  
    insert into Categories(PARENTID,Category,Date) values(@item,@category,@date)  
  end  
end

i have Categories table which has got 3 columns: PARENTID,Category,Date

我有类别表有3列:PARENTID,类别,日期

2 个解决方案

#1


It looks as if your EF data model and your database are not in sync anymore. It seems as if your "Categories" object in the EF data model has an "ID" field but the table does not.

看起来您的EF数据模型和数据库不再同步。好像EF数据模型中的“类别”对象有一个“ID”字段但表没有。

I would update the EF data model from the database and see if that fixes the problem. To do this, open up the EDMX designer and right click on an empty spot in the design surface, and pick the "Update model from database" option. That should bring the two worlds back into sync.

我会从数据库更新EF数据模型,看看是否能解决问题。为此,打开EDMX设计器并右键单击设计图面中的空白点,然后选择“从数据库更新模型”选项。这应该会使两个世界重新同步。

Marc

#2


Mitch Wheat gave you the answer. You're trying to use the ID column, but the table has a PARENTID column.

米奇小麦给了你答案。您正在尝试使用ID列,但该表具有PARENTID列。

#1


It looks as if your EF data model and your database are not in sync anymore. It seems as if your "Categories" object in the EF data model has an "ID" field but the table does not.

看起来您的EF数据模型和数据库不再同步。好像EF数据模型中的“类别”对象有一个“ID”字段但表没有。

I would update the EF data model from the database and see if that fixes the problem. To do this, open up the EDMX designer and right click on an empty spot in the design surface, and pick the "Update model from database" option. That should bring the two worlds back into sync.

我会从数据库更新EF数据模型,看看是否能解决问题。为此,打开EDMX设计器并右键单击设计图面中的空白点,然后选择“从数据库更新模型”选项。这应该会使两个世界重新同步。

Marc

#2


Mitch Wheat gave you the answer. You're trying to use the ID column, but the table has a PARENTID column.

米奇小麦给了你答案。您正在尝试使用ID列,但该表具有PARENTID列。