Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Map a VIEW without unique identifing column with Fluent Nhibernate

i have readonly VIEWs in an existing Database and i'd like to get them with FHN. i tried mapping it the following way:

public class HhstMap : ClassMap<Hhst>
{
    public HhstMap()
    {
        Table("HHST");

        ReadOnly();

        Id();

        Map(x => x.Hkz);
        Map(x => x.Kapitel);
        Map(x => x.Titel);
        Map(x => x.Apl);
        Map(x => x.Hhpz);
    }
}

but i got an error: could not execute query [ SELECT this_.id as id3_0_, this_.Hkz as Hkz3_0_, this_.Kapitel as Kapitel3_0_, this_.Titel as Titel3_0_, this_.Apl as Apl3_0_, this_.Hhpz as Hhpz3_0_ FROM HHST this_ ]

this is ok cause there is no ID Column, but how can i do mapping with Fluent without the ID?

like image 505
blindmeis Avatar asked Sep 01 '10 12:09

blindmeis


2 Answers

You could retrieve the records as value objects (non-managed entities) instead of entities.

"14.1.5. Returning non-managed entities It is possible to apply an IResultTransformer to native sql queries. Allowing it to e.g. return non-managed entities.

sess.CreateSQLQuery("SELECT NAME, BIRTHDATE FROM CATS")
    .SetResultTransformer(Transformers.AliasToBean(typeof(CatDTO)))

This query specified: the SQL query string a result transformer

The above query will return a list of CatDTO which has been instantiated and injected the values of NAME and BIRTHNAME into its corresponding properties or fields. "

like image 167
J Fernandes Avatar answered Dec 03 '22 06:12

J Fernandes


Maybe this can help you: Fluent nHibernate no identity column in table…?

EDIT: Also, you could use a composite id, or maybe you need the latest version of Fluent Nhibernate?

like image 45
Peter Avatar answered Dec 03 '22 07:12

Peter