Dapper和Oracle CRUD问题,如何?

时间:2012-03-20 15:28:16

标签: c#-4.0 crud dapper

如何让Dapper.NET成为CRUD我的Oracle DB?

我有一个名为PLAYER_LOG的表,它的标识是由触发器完成的,这里是sql

SELECT SQ_MASTER_SEQUENCE.NEXTVAL INTO tmpVar FROM dual;
:NEW.ID := tmpVar;

我的模特是:

public class PlayerLogStorage : IEntity //-> here is the identity
{       
    public string Cli { get; set; }
    public string PlayerAnswer { get; set; }
    public DateTime InsertDate { get; set; }
}

这是我的插入内容:

 using (IDbConnection ctx = DbConnectionProvider.Instance.Connection)
 {
            ctx.Query<PlayerLogStorage>("INSERT INTO PLAYER_LOG (CLI, ANSWER, INSERT_DATE) VALUES (:Cli, :PlayerAnswer, :InsertDate)", new
            {
                Cli = model.Cli,
                PlayerAnswer = model.PlayerAnswer,
                InsertDate = model.InsertDate
            });
 }

这是例外:

ORA-01008: not all variables bound

2 个答案:

答案 0 :(得分:11)

我遇到了类似的东西,但是使用了return语句。我发现的技巧是使用DynamicParameters对象。在我使用的系统中,insert语句必须在序列上调用NextVal,它不在触发器中。

var param = new DynamicParameters();

param.Add(name: "Cli", value: model.Cli, direction: ParameterDirection.Input);
param.Add(name: "PlayerAnswer", value: model.PlayerAnswer, direction: ParameterDirection.Input);
param.Add(name: "InsertDate", value: model.InsertDate, direction: ParameterDirection.Input);
param.Add(name: "Id", dbType: DbType.Int32, direction: ParameterDirection.Output);

using (IDbConnection ctx = DbConnectionProvider.Instance.Connection)
{
    ctx.Execute("INSERT INTO PLAYER_LOG (CLI, ANSWER, INSERT_DATE) VALUES (:Cli, :PlayerAnswer, :InsertDate) returning Id into :Id", paramList);
}

var Id = param.get<int>("Id");

答案 1 :(得分:6)

除了bwalk2895的答案之外,您还可以将模型对象传递给DynamicParameters的构造函数,然后您只需要添加输出参数。保存几行代码,尤其是对于具有许多属性的对象。例如:

var param = new DynamicParameters(model);

param.Add(name: "Id", dbType: DbType.Int32, direction: ParameterDirection.Output);

using (IDbConnection ctx = DbConnectionProvider.Instance.Connection)
{
    ctx.Execute("INSERT INTO PLAYER_LOG (CLI, ANSWER, INSERT_DATE) VALUES (:Cli, :PlayerAnswer, :InsertDate) returning Id into :Id", param);
}

var Id = param.Get<int>("Id");

更新:更正的方法名称