NHibernate Insert into … select … with GUID as PrimaryKey

二次信任 提交于 2019-12-11 04:49:39

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/11528914/nhibernate-insert-into-select-with-guid-as-primarykey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!