2012-07-17 18 views
1

我嘗試做與NHibernate休耕:NHibernate的INSERT INTO ... SELECT ...用GUID作爲PrimaryKey的

this.Session.CreateQuery(@"insert into ContactGroupContact (Id, MailAddress, Company, Person, Branch, ContactGroup, User, FaxNumber) 
          select newid(), MailAddress, Company, Person, Branch, 
            :destContactGroupId, User, FaxNumber 
          from ContactGroupContact cgc 
          where cgc.ContactGroup.Id = :contactGroupId") 
     .SetEntity("destContactGroupId", tempContactGroup) 
     .SetGuid("contactGroupId", contactGroupId) 
     .ExecuteUpdate(); 

列的ContactGroupContactIdGUID型。

當我執行此,得到了一個NHibernate.QueryException使用以下信息:

節點沒有任何數據類型:MethodNode((NEWID exprList)[插入 ContactGroupContact(ID,MailAddress,將公司,人,科, ContactGroup,用戶,FaxNumber) SELECT NEWID(),MailAddress,將公司,人,科,:destContactGroupId,用戶,FaxNumber 從ContactGroupContact CGC 其中cgc.ContactGroup.Id =:contactGroupId]

有人可以幫助我,出了什麼問題? - 謝謝。

回答

1

嘗試創建派生Dialect和註冊newid爲函數。

1

這裏是迭戈的建議的一個例子。我承認我是新來這一點,但假設這是處理它適當的方式(它的工作原理)

public class DerivedDialectWithNewId : MsSql2005Dialect 
{ 
    public DerivedDialectWithNewId() 
    { 
     RegisterFunction("newid", new NoArgSQLFunction("newid", NHibernateUtil.Guid, true));   
    } 
} 

,這裏是如何與功能NHibernate使用

private static FluentConfiguration GetConfiguration() 
    { 
     return Fluently.Configure() 
      .Database(
       MsSqlConfiguration.MsSql2005.ConnectionString(
        c => c.FromConnectionStringWithKey("CustomConnectionString")) 
        .Dialect<DerivedDialectWithNewId>())); 
    } 
+0

你將如何把這種使用LINQ? – nfplee 2013-07-22 14:41:55