Dapper和Oracle CRUD問題,怎麼樣?

c#-4.0 crud dapper

如何讓Dapper.NET成為CRUD我的Oracle數據庫?

我有一個名為: 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

一般承認的答案

我遇到了類似的東西,但使用了返回語句。我找到的技巧是使用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");

熱門答案

除了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");

更新:更正的方法名稱



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因