How to map a view without an identity column in NHibernate?

1,791 views
Skip to first unread message

Farzad

unread,
Dec 20, 2011, 8:26:44 PM12/20/11
to nhusers
# I have already posted this in stackoverflow:
http://stackoverflow.com/questions/8567099/how-to-map-a-view-without-an-identity-column-in-nhibernate

I have a view that I am going to only read from (no writes). This view
does not have any unique key (not event composite).

So how can I map this view in NHibernate without touching the view? I
do not want to add a new column to the view to generate a unique
identity for me. Is there a way to map this view and generate the
identity column on the NHibernate side?

I can generate a GUID in my entity class like:

public class MyViewClass
{
private Guid _id = new Guid();
public virtual Guid Id { get { return _id; } set { _id =
value; } }
}
But how can I make the mapping work? The following code does not work:

public class MyViewClass: ClassMapping<MyViewClass>
{
public MyViewClass()
{
Mutable(false);
Id(x => x.Id, m => m.Generator(Generators.Guid));
}

}
It expects to have the Id column in view and throws:

System.Data.SqlClient.SqlException: Invalid column name 'Id'.
BTW, I am using NHibernate 3.2 and mapping by code.

Also I want to use LINQ to NHibernate so I cannot use custom sql
queries.

Itzik Saban

unread,
Dec 21, 2011, 1:46:40 AM12/21/11
to nhu...@googlegroups.com
Let's see if i got you right - it is possible that 2 or more records will be identical in each and every column?
Or maybe the primary key is a composition of all the columns together? if so, you can create a composite id in your mapping that will consists of all your columns/fields.

2011/12/21 Farzad <farzad...@gmail.com>

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



--
Itzik Saban


Farzad

unread,
Dec 21, 2011, 3:45:44 PM12/21/11
to nhusers
Yea, the unique key would be a composition of all the columns.
I haven't tried this approach because I thought there should be a
better way.
I am not sure how this will affect the performance on reads. Is there
a considerable overhead with such a big composite key?

On Dec 20, 10:46 pm, Itzik Saban <itziksa...@gmail.com> wrote:
> Let's see if i got you right - it is possible that 2 or more records will
> be identical in each and every column?
> Or maybe the primary key is a composition of all the columns together? if
> so, you can create a composite id in your mapping that will consists of all
> your columns/fields.
>
> 2011/12/21 Farzad <farzad.pan...@gmail.com>
>
>
>
>
>
>
>
>
>
> > # I have already posted this in stackoverflow:
>
> >http://stackoverflow.com/questions/8567099/how-to-map-a-view-without-...
> my blog <http://itziksaban.blogspot.com/>

eyar

unread,
Dec 22, 2011, 12:10:25 AM12/22/11
to nhusers
hi
if you use SQLServer 2005 or higher, this select create unique
identifier for map in NHibernate:

SELECT ROW_NUMBER() OVER (ORDER BY [AnyColumnName]) AS [Id], * FROM
[Table]

Reply all
Reply to author
Forward
0 new messages