BennoDual
BennoDual

Reputation: 6259

NHibernate Insert into ... select ... with GUID as PrimaryKey

I try to do fallowing with NHibernate:

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();

The column Id of ContactGroupContact is of type GUID.

When I execute this, I get a NHibernate.QueryException with the following message:

No data type for node: MethodNode ( ( newid exprList ) [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]

Can someone help me, what is going wrong? - Thanks.

Upvotes: 1

Views: 902

Answers (2)

Eric LaForce
Eric LaForce

Reputation: 2151

Here is an example of Diego's suggestion. I admit I am new to this, but assuming this is the appropriate way to handle it (it works)

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

and here is how to use it with Fluent NHibernate

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

Upvotes: 1

Diego Mijelshon
Diego Mijelshon

Reputation: 52725

Try creating a derived Dialect and registering newid as a function.

Upvotes: 1

Related Questions