System.Data.Entity.Infrastructure.DbRawSqlQuery ' 1 [System.
本文关键字:System DbRawSqlQuery Data Entity Infrastructure | 更新日期: 2023-09-27 18:05:48
我正在尝试创建一个新记录,并不断得到一个错误
System.Data.Entity.Infrastructure.DbRawSqlQuery ' 1(系统。[Int32]'输入'系统。IConvertible
我有这段代码…
// Contact
c.Name = IsItNull(contact.Name);
int newContactID = AddContact(c);
AddContact方法是
public int AddContact(Contact contact)
{
return Convert.ToInt32(AWJE.Database.SqlQuery<int>
("usp_InsertContact @Name", contact.Name));
}
storedproc
CREATE PROCEDURE [dbo].[usp_InsertContact]
@ContactID int output,
@Name nvarchar(50)
AS
SET NOCOUNT OFF
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
DECLARE @ERROR_SEVERITY int,
@MESSAGE varchar(1000),
@ERROR_NUMBER int,
@ERROR_PROCEDURE nvarchar(200),
@ERROR_LINE int,
@ERROR_MESSAGE nvarchar(4000);
begin try
insert into [Contact]
(Name) values (@Name)
set @ContactID = @@IDENTITY
end try
BEGIN CATCH
SET @ERROR_SEVERITY = ISNULL(ERROR_SEVERITY(),'');
SET @ERROR_NUMBER = ISNULL(ERROR_NUMBER(),'');
SET @ERROR_PROCEDURE = ISNULL(ERROR_PROCEDURE(),'');
SET @ERROR_LINE = ISNULL(ERROR_LINE(),'');
SET @ERROR_MESSAGE = ISNULL(ERROR_MESSAGE(),'');
-- Test if the transaction is uncommittable.
IF (XACT_STATE()) = -1
BEGIN
--PRINT N'The transaction is in an uncommittable state. Rolling back transaction.'
ROLLBACK TRANSACTION;
END;
-- Test if the transaction is active and valid.
IF (XACT_STATE()) = 1
BEGIN
--PRINT N'The transaction is committable. Committing transaction.'
COMMIT TRANSACTION;
END;
SET @MESSAGE = 'Error Occured in Stored Procedure ' + cast(@ERROR_PROCEDURE as varchar(200)) +
'; Line Number ' + cast(@ERROR_LINE as varchar) +
'; Message: [' + cast(@ERROR_NUMBER as varchar) + '] - '
+ cast(@ERROR_MESSAGE as varchar(255))
RAISERROR(@MESSAGE, @ERROR_SEVERITY, 1);
END CATCH;
GO
在
抛出错误int newContactID = AddContact(c);
我到底做错了什么?我看过其他的SO帖子,这有点类似于这个错误,但他们是关于Linq的,不像我是怎么做的。知道的吗?
我发现了问题,问题在AddContact方法
public int AddContact(Contact contact)
{
return Convert.ToInt32(AWJE.Database.SqlQuery<int>
("usp_InsertContact @Name", contact.Name));
}
而不是使用SqlQuery,我使用ExecuteSqlCommand在其位置,这修正了错误。现在这个方法看起来是这样的。
return AWJE.Database.ExecuteSqlCommand
("usp_InsertContact @Name", contact.Name);