Automapping primary key as database assigned - GeneratedBy.Identity()

546 views
Skip to first unread message

Sofija Blazevski

unread,
Mar 9, 2012, 1:24:55 PM3/9/12
to fluent-n...@googlegroups.com
I'm exploring FluentNHibernate, and I love convention over configuration and automapping features, but i have a trouble when I want my identity property not to be in insert sql statement generated. I want primary key to be assigned by database.

For example:

instead of 

insert into Product(ProductId, Name) values (1, 'Test');

sql generated should be 

insert into Product(Name) values ('Test'); 

What I tried is adding convention like 

public class PrimaryKeyConvention : IIdConvention
    {
        public void Apply(IIdentityInstance instance)
        {            
            instance.Column(instance.EntityType.Name + "Id");     
            instance.GeneratedBy.Identity();
        }
    }

but that doesn't work. Should I use some other interface, or there is something else needed for this?


Sofija Blazevski

unread,
Mar 13, 2012, 5:16:05 PM3/13/12
to Fluent NHibernate, Sofi
I'm a bit surprised no one answered. Anyway, I just wanted to correct
myself, as I actually wanted to insert into a view, not a table. So,
conclusion on this is that FluentNHibernate generates correct xml
file, but NHibernate doesn't generate expected sql if an insert is
made on a view. And that is for both GeneratedBy.Native and
GeneratedBy.Identity.



On Mar 9, 7:24 pm, Sofija Blazevski <sosin...@gmail.com> wrote:
> I'm exploring FluentNHibernate, and I love convention over configuration
> and automapping features, but i have a trouble when I want my identity
> property not to be in insert sql statement generated. I want primary key to
> be assigned by database.
>
> For example:
>
> instead of
>
> insert into Product(ProductId, Name) values (1, 'Test');
>
> sql generated should be
>
> insert into Product(Name) values ('Test');
>
> What I tried is adding convention like
>
> public class PrimaryKeyConvention : IIdConvention
>     {
>         public void Apply(IIdentityInstance instance)
>         {
>             instance.Column(instance.EntityType.Name + "Id");
>             *instance.GeneratedBy.Identity();*

Brad Laney

unread,
Mar 14, 2012, 1:10:19 AM3/14/12
to fluent-n...@googlegroups.com
Pretty sure NH doesn't know if its a view or a table.
NH is a ORM that does not know of the databases existence. 
You tell it what to do and it does it ignoring the settings on the db.
I have this setup and it works fine. I'll reply with our primary key convention tomorrow.

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.


Sofi

unread,
Mar 14, 2012, 4:34:30 AM3/14/12
to fluent-n...@googlegroups.com
I was expecting same thing - views should be treated just as if they were tables, that's why at first I gave example with table. I thought I was doing something wrong.
When I switched inserting into a table, without changing convention, everything was fine.

NH throws error like "null identifier" or "null id" in case of a view. It doesn't generate sql.

Brad Laney

unread,
Mar 15, 2012, 2:12:11 AM3/15/12
to fluent-n...@googlegroups.com
I forgot to get the code for you again, my bad. I'll try to remember tomorrow.
Reply all
Reply to author
Forward
0 new messages