如何使用EF Power tools向API控制器添加插入存储过程?



我使用EF Core电源工具生成下面的代码,并试图创建一个可以调用InsertAttendee存储过程的控制器。

我不确定如何确切地做到这一点,我所知道的是我需要调用InsertAttendeeAsync方法。我不确定如何处理id,返回值和cancellationtoken,因为它们是我以前从未处理过的输出参数。任何帮助都会很感激。谢谢。

[HttpPost("InsertAttendee")]
public async Task<int> InsertAttendeeAsync(Attendee1 request)
{
Attendee1 Attendee1 = new Attendee1();
Attendee1.Type = request.Type;
Attendee1.Person = request.Person;
Attendee1.FirstName = request.FirstName;
Attendee1.LastName = request.LastName;
Attendee1.DateCreated = request.DateCreated;
Attendee1.DateModified = request.DateModified;
Attendee1.ModifiedBy = request.ModifiedBy;
int response = await _context.GetProcedures().InsertAttendeeAsync(Attendee1.DateCreated, Attendee1.DateModified, Attendee1.FirstName, Attendee1.LastName, Attendee1.ModifiedBy, Attendee1.Person, Attendee1.Type, Attendee1.id, Attendee1.returnvalue, Attendee1.cancellationtoken);
return response;   
}
namespace *****_API.Models
{
public partial class Attendee1
{
public int Id { get; set; }
public int Type { get; set; }
public int? Person { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public string ModifiedBy { get; set; }
public virtual Person PersonNavigation { get; set; }
public virtual AttendeeType TypeNavigation { get; set; }
public virtual ExamAttendee1 ExamAttendee1 { get; set; }
public virtual MeetingAttendee MeetingAttendee { get; set; }
}
}

KATSContextProcedures

public virtual async Task<int> InsertAttendeeAsync(DateTime? dateCreated, DateTime? dateModified, string firstName, string lastName, string modifiedBy, int? person, int? type, OutputParameter<int?> id, OutputParameter<int> returnValue = null, CancellationToken cancellationToken = default)
{
var parameterid = new SqlParameter
{
ParameterName = "id",
Direction = System.Data.ParameterDirection.InputOutput,
Value = id?._value ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.Int,
};
var parameterreturnValue = new SqlParameter
{
ParameterName = "returnValue",
Direction = System.Data.ParameterDirection.Output,
SqlDbType = System.Data.SqlDbType.Int,
};
var sqlParameters = new []
{
new SqlParameter
{
ParameterName = "dateCreated",
Value = dateCreated ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.DateTime,
},
new SqlParameter
{
ParameterName = "dateModified",
Value = dateModified ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.DateTime,
},
new SqlParameter
{
ParameterName = "firstName",
Size = 100,
Value = firstName ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.VarChar,
},
parameterid,
new SqlParameter
{
ParameterName = "lastName",
Size = 100,
Value = lastName ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.VarChar,
},
new SqlParameter
{
ParameterName = "modifiedBy",
Size = 50,
Value = modifiedBy ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.VarChar,
},
new SqlParameter
{
ParameterName = "person",
Value = person ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.Int,
},
new SqlParameter
{
ParameterName = "type",
Value = type ?? Convert.DBNull,
SqlDbType = System.Data.SqlDbType.Int,
},
parameterreturnValue,
};
var _ = await _context.Database.ExecuteSqlRawAsync("EXEC @returnValue = [Requirements].[InsertAttendee] @dateCreated, @dateModified, @firstName, @id OUTPUT, @lastName, @modifiedBy, @person, @type", sqlParameters, cancellationToken);
id.SetValue(parameterid.Value);
returnValue?.SetValue(parameterreturnValue.Value);
return _;
}

IKATSContextProcedures

Task<int> InsertAttendeeAsync(DateTime dateCreated, DateTime dateModified, string firstName, string lastName, string modifiedBy, int? person, int type, int id);

. NET应该能够通过依赖注入为你创建一个cancellationToken。

您可以使用如下样例代码中的输出参数:

[HttpPost("InsertAttendee")]
public async Task<int> InsertAttendeeAsync(Attendee1 request, CancellationToken cancellationToken)
{
Attendee1 Attendee2 = new Attendee1();
Attendee1.Type = request.Type;
Attendee1.Person = request.Person;
Attendee1.FirstName = request.FirstName;
Attendee1.LastName = request.LastName;
Attendee1.DateCreated = request.DateCreated;
Attendee1.DateModified = request.DateModified;
Attendee1.ModifiedBy = request.ModifiedBy;
var returnedId = new OutputParameter<int?>();
int response = await _context.GetProcedures().InsertAttendeeAsync(Attendee2.DateCreated, Attendee2.DateModified, Attendee2.FirstName, Attendee2.LastName, Attendee2.ModifiedBy, Attendee2.Person, Attendee2.Type, returnedId, cancellationtoken);
return response;   
}

相关内容

  • 没有找到相关文章

最新更新