int does not contain a definition for Getawaiter + asp.net core web api async

时间:2021-02-06 16:57:54

I am trying to implement the async CRUD functionality for my application using asp.net core webapi. I have been told to only use stored procedures for doing any requests to the database. I have implemented the async for the get request but struggling to the same for create request. Could somebody tell me what am I doing wrong in my code?

我正在尝试使用asp.net核心webapi为我的应用程序实现异步CRUD功能。我被告知只使用存储过程来对数据库进行任何请求。我已经为get请求实现了异步,但是对于创建请求却一直在努力。有人能告诉我在我的代码中我做错了什么吗?

I am getting the following error message in my repository method in the await line of code

我在await代码行中的存储库方法中收到以下错误消息

int does not contain a definition for Getawaiter + asp.net core web api async

Controller method:

控制器方法:

[HttpPost]
[Route("api/Movies")]
public async Task<IActionResult> CreateMovie([FromBody] MoviesDto movies)
{
    if (movies == null)
    {
        return BadRequest();
    }

    // Check if movie exists
    var movie =  _moviesRepository.GetMovie(movies.MovieId);

    if (movie == null)
    {
        return NotFound();
    }

    var results = Mapper.Map<Movies>(movies);

    if (ModelState.IsValid)
    {
        await _moviesRepository.AddMovie(results);
    }

    return Ok(results);
}

Repository method:

存储库方法:

public async Task AddMovie(Movies movie)
{
    await _mrdbContext.Database.ExecuteSqlCommand("dbo.spInsertMovie {0}, {1}, {2}, {3}, {4}, {5}", movie.Title,movie.ReleaseYear,movie.Plot,movie.MovieLength,"Ranjit Menon","");
}

New repository method:

新的存储库方法:

public async Task AddMovie(Movies movie)
{
    object[] parameters =
            {
                new SqlParameter("@val1", movie.Title),
                new SqlParameter("@val2", movie.ReleaseYear),
                new SqlParameter("@val3", movie.Plot),
                new SqlParameter("@val4", movie.MovieLength),
                new SqlParameter("@val5", "Ranjit Menon"),

                new SqlParameter
                {
                    ParameterName = "@retVal",
                    SqlDbType = SqlDbType.Int,
                    Direction = ParameterDirection.Output,
                    Value = -1
                }
        };

    await  _mrdbContext.Database.ExecuteSqlCommandAsync("EXEC  @retVal = dbo.spInsertMovie @val1, @val2, @val3, @val4,@val5", CancellationToken.None , parameters);
}

Stored procedure:

存储过程:

CREATE PROCEDURE [dbo].[spInsertMovie]
    (@Title varchar(50),  
     @ReleaseYear int, 
     @Plot varchar(50), 
     @MovieLength int, 
     @CreatedBy varchar(20)
    )
AS
BEGIN
    set nocount on

    ----------------------------------------
    -- variables
    ----------------------------------------

    -- error
    declare @ErrorMessage nvarchar(2048), @ErrorSeverity int, @ErrorState int, @MovieID int

    ----------------------------------------
    -- insert record
    ----------------------------------------

    begin try


        -- insert record
            insert into [dbo].[Movies]
                       ([Title]
                       ,[ReleaseYear]
                       ,[Plot]
                       ,[MovieLength]                      
                       ,[CreatedBy]                    
                       ,[UpdatedBy])
            values
                       (@Title
                       ,@ReleaseYear
                       ,@Plot
                       ,@MovieLength                     
                       ,@CreatedBy
                       ,@CreatedBy)


        -- get assigned id
        set @MovieID = scope_identity()



    end try
    begin catch
        select @ErrorMessage = dbo.fnGetErrorMessage('Insert Movie Record', error_message(), object_schema_name(@@procid), object_name(@@procid), error_line()), @ErrorSeverity = error_severity(), @ErrorState = error_state();
        raiserror(@ErrorMessage, @ErrorSeverity, @ErrorState);
        return
    end catch

    -- return assigned id
    select @MovieID as MovieID

END

1 个解决方案

#1


1  

ExecuteSqlCommand most likely returns an int, not a Task. Use ExecuteSqlCommandAsync instead. https://msdn.microsoft.com/en-us/library/system.data.entity.database.executesqlcommandasync(v=vs.113).aspx

ExecuteSqlCommand很可能返回一个int,而不是一个Task。请改用ExecuteSqlCommandAsync。 https://msdn.microsoft.com/en-us/library/system.data.entity.database.executesqlcommandasync(v=vs.113).aspx

#1


1  

ExecuteSqlCommand most likely returns an int, not a Task. Use ExecuteSqlCommandAsync instead. https://msdn.microsoft.com/en-us/library/system.data.entity.database.executesqlcommandasync(v=vs.113).aspx

ExecuteSqlCommand很可能返回一个int,而不是一个Task。请改用ExecuteSqlCommandAsync。 https://msdn.microsoft.com/en-us/library/system.data.entity.database.executesqlcommandasync(v=vs.113).aspx