2011-08-11 3 views
8

이 같은 LINQ 쿼리와 NHibernate에 대한 리포 지 토리가 LINQ를 사용하여 NHibernate 효율적인 삭제 조건

var q = from x in SomeIQueryable<SomeEntity> where x.A1 == a1 && x.B1 == b1 select x; 

이 WHERE 필터를 얻고 HQL을 통해서만 가능할 것으로 보이는 "one-shot-delete"에 적용하는 방법이 있습니까?

var cmd = string.Format("delete from SomeEntity where x.A1 = '{0}' and x.B1 = {1}", a1, b1); 
session.CreateQuery(cmd).ExecuteUpdate(); 

답변

6

NH LINQ 공급자 및 조건/queryover API는 조건부 삭제/업데이트를 지원하지 않습니다. NHibernate 확장을 고려하지 않는 한 HQL 또는 원시 SQL이 유일한 옵션입니다.

0
(from x in NHSession.Query<SomeEntity>() 
        where x.A1 == a1 && x.B1 == b1 
        select x).ForEach(y => { NHSession.Delete(y) }); 
     NHSession.Flush(); 
+1

몇 가지 설명을 게시하시기 바랍니다; 코드 만의 대답은별로 말하지 않습니다. – rgettman

+0

이 문제는 모든 엔터티를 삭제하기 만하면 해당 엔터티를로드한다는 점에서 문제가 있습니다. –

+0

이것은 우리가 효율적이라고 부르는 것이 아닙니다 – Beatles1692

1

현재 NH 4.0.1 현재로서는 불가능합니다. 그러나 Jira (NH-3659, https://nhibernate.jira.com/browse/NH-3659)에는 공개 문제가 있습니다. http://weblogs.asp.net/ricardoperes/strongly-typed-delete-with-nhibernate에 설명 된 사용자 지정 인터셉터와 SQL 대체를 기반으로하는 해킹 된 솔루션이 있지만 깨끗한 솔루션을 개발 중이며 결국 끌어 오기 요청을 제출합니다.

+0

NH-3659에서 일하고 있다는 것을 잊어 버렸지 만 지금까지 보여줄 것이 없습니다. 이 게시물에 언급 된 몇 가지 대안이 있습니다 : https://weblogs.asp.net/ricardoperes/deleting-entities-in-nhibernate (면책 조항 : 내 자신의) –

+0

NH-3659에 대한 요청을 제출했습니다 - Strong Typed 지우다. 링크는 https://nhibernate.jira.com/browse/NH-3659에서 확인할 수 있습니다. –

0

그것은 NHibernate에 5.0 가능해 :

session.Query<SomeIQueryable>() 
      .Where(x => x.A1 == a1 && x.B1 == b1) 
      .Delete(); 

문서 :

// 
    // Summary: 
    //  Delete all entities selected by the specified query. The delete operation is 
    //  performed in the database without reading the entities out of it. 
    // 
    // Parameters: 
    // source: 
    //  The query matching the entities to delete. 
    // 
    // Type parameters: 
    // TSource: 
    //  The type of the elements of source. 
    // 
    // Returns: 
    //  The number of deleted entities. 
    public static int Delete<TSource>(this IQueryable<TSource> source); 
관련 문제