2017-04-13 54 views
1

假設我的實體是如何讓Dapper Dommel插入應用程序生成的主鍵?

public class AppUser 
{ 
    string Id { get; set; } 
    string Name { get; set; } 
} 

它看起來像默認短小精悍Dommel不插入ID字段。它會生成SQL非常接近這個:

insert into AppUser (Name) values ("Dapper") select cast(scope_identity() as int) 

這個SQL是通過使用小巧精緻的Dommel插入功能,像這樣產生的:

using (var connection = new System.Data.SqlClient.SqlConnection(ConnectionString)) 
{ 
    connection.Open(); 
    connection.Insert<AppUser>(new User { Id = "someGuid", Name = "Dapper" }); 
} 

我想它插入我所提供的ID列一個值,並且也不執行select cast(scope_identity()as int)查詢。也就是說,我想這樣的事情

insert into AppUser (Id, Name) values ("someGuid", "Dapper") 

我似乎無法在文檔中找到此。有誰知道如何做到這一點?

回答

0

我不熟悉Dommel,但你可以,請使用.execute:

_dbConnection.Execute(@"insert into AppUser (Id, Name) 
          values (@Id, @Name)", new {Id = 1, Name = "Foo"}); 
+0

是的,你是絕對正確的,但不幸的是,因爲它使用類似的方法插入它會破壞Dommel的目的(目標) ,刪除(對象),更新(對象) - 沒有硬編碼SQL的好處。 –