过程或函数需要未提供的参数

时间:2014-07-17 12:33:15

标签: asp.net-mvc-4 stored-procedures repository-pattern dapper unit-of-work

我的存储过程是

Create proc [dbo].[InsertPerson]
(
    @LastName   VARCHAR(64),
    @FirstName  VARCHAR(64),
    @Age        INT
)
AS
INSERT INTO Person Values(@LastName,@FirstName,@Age)

BaseRepository类的save方法如下:

public virtual int Add(string storedProcedure, T entity, IDbTransaction transaction)
 {
    var i = 0;

    try
    {

       if (entity == null)
          throw new ArgumentNullException("entity", "Add to DB null entity");

       var parameters = (object)Mapping(entity);
       i = _conn.Execute(storedProcedure, parameters, transaction, commandType: CommandType.StoredProcedure);

    }
    catch (Exception ex)
    {

       throw ex;

    }

    return i;
 }

地图方法如下:

internal virtual dynamic Mapping(T entity)
{
  return entity;
}

PersonRepository类的定义如下:

public class PersonRepository:DapperRepository<Person>, IPersonRepository
{
   public PersonRepository(IUnitOfWork uow) : base(uow) { }

   internal override dynamic Mapping(Person person)
   {
      return new {person.LastName, person.FristName, person.Age };
   }
}

我的服务类我将Save方法声明如下:

public void Save(Person person)
{
  try
  {
    string sp = "InsertPerson";
    _repository.Add(sp, person, _unitOfWork.BeginTransaction());
    _unitOfWork.Commit();
  }
  catch (Exception)
  {

     throw;
  }
}

当我尝试执行我的Save方法时,它会向我显示以下异常:

Procedure or function 'InsertPerson' expects parameter '@FirstName', which was not supplied.

1 个答案:

答案 0 :(得分:2)

您的媒体资源名称存在拼写错误

person.FristName

应该是

person.FirstName

所以现在您的映射函数应该如下所示

internal override dynamic Mapping(Person person)
{
      return new {person.LastName, person.FirstName, person.Age };
}