Asp.net mvc 4 过程或函数需要未提供的参数
我的存储过程是Asp.net mvc 4 过程或函数需要未提供的参数,asp.net-mvc-4,stored-procedures,repository-pattern,dapper,unit-of-work,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类的保存方法如下: public virtual int Add(string storedProcedure,
Create proc [dbo].[InsertPerson]
(
@LastName VARCHAR(64),
@FirstName VARCHAR(64),
@Age INT
)
AS
INSERT INTO Person Values(@LastName,@FirstName,@Age)
BaseRepository类的保存方法如下:
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;
}
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 };
}
}
public void Save(Person person)
{
try
{
string sp = "InsertPerson";
_repository.Add(sp, person, _unitOfWork.BeginTransaction());
_unitOfWork.Commit();
}
catch (Exception)
{
throw;
}
}
map方法如下所示:
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;
}
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 };
}
}
public void Save(Person person)
{
try
{
string sp = "InsertPerson";
_repository.Add(sp, person, _unitOfWork.BeginTransaction());
_unitOfWork.Commit();
}
catch (Exception)
{
throw;
}
}
PersonRepository类的定义如下:
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;
}
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 };
}
}
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.
您的物业名称有拼写错误
person.FristName
应该是
person.FirstName
现在映射函数应该是这样的
internal override dynamic Mapping(Person person)
{
return new {person.LastName, person.FirstName, person.Age };
}
请检查我的答案……:)我建议不要在数据库中存储一个人的年龄。年龄不是一个静态数据点,它会根据当前日期进行更改。取而代之的是,记录他们的出生日期,并在需要时计算他们的年龄。