2013-04-26 1 views
0

Dapper는이 쿼리를 car 객체에 매핑 할 수 있습니다. 자동차의 어느 속성이 쿼리에서 어떤 변수로 이동하는지 알 수 있습니다.업데이트 쿼리에서 두 객체 매핑

Car car = new Car(); 
conn.Execute(
     "UPDATE CAR" + 
     " SET [email protected],[email protected]" + 
     " WHERE [email protected]", car; 

하지만 다른 변수를 쿼리에 추가 할 수 있습니까? 예 :

int c = 1000; 
conn.Execute(
     "UPDATE CAR" + 
     " SET [email protected],[email protected],[email protected]" + 
     " WHERE [email protected]", car, new { cost = c }; 

자동차 개체의 모든 속성을 검색어의 변수와 일치시키지 않고도이 작업을 수행하고 싶습니다. 여기에 예제 클래스가 있지만 실제 클래스에는 더 많은 속성이 있습니다.

public class Car { 
    public int Id { get; set; } 
    public string Color { get; set; } 
    public string Model { get; set; } 
} 

답변

1

. 예 :

var car = new Car(); 
car.Color = "Black"; 
car.Model = "BMW"; 
car.Id = 123; 
var cost = 53000 

DbConnection.Execute(@"update car set [email protected], model = @model, cost = @cost where id = @id", 
       new { color = car.Color, model = car.Model, cost = cost, id=car.Id}); 

이렇게하면 여러 개체/변수에서 동적을 조합 할 수 있습니다.