Search code examples
c#visual-studio-2012nhibernatecastle-activerecord

Castle ActiveRecord mapping a view


Hello I'm using VS2012 c# and Castle Active Record.

After reading this

How to map a database view using ActiveRecord?

And this

mapping view using castle activerecord, exception on primary key

And this

Castle ActiveRecord Error With View: NHibernate.PropertyNotFoundException for generated key

I still have no idea how to solve the same issue everyone in these questions has. Without adding a composite key how can I map a view in Castle ActiveRecords? There is mention of a simple primary key but what does that mean? How do I set that up? Or is there another way?

The table the view is based on has a primary key column but when I map it as primary key it will throw an error Could not perform FindAll. When I open it up the inner exception is null. There is nothing...

Hey Dirk thanks for answering here is the view map

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

namespace Data_Objects.CAR.Klase
{
    [ActiveRecord("OSNOVNASREDSTVA")]
    public class OsnovnaSredstva : ActiveRecordBase<OsnovnaSredstva>
    {
        private int _ossr_sifra;
        private string _ossr_naziv;
        private string _ossr_opis;
        private int _ossr_stanje;
        private int _ossr_org;
        private int _ossr_lok;
        private DateTime _ossr_odDatuma;



        [PrimaryKey(PrimaryKeyType.Native, Column = "SIFRA")]
        public int Ossr_Sifra
        {
            get { return _ossr_sifra; }
            set { _ossr_sifra = value; }
        }


    }
}

I have other attributes that i deleted for the moment testing to make sure the issue is with the primary key attribute (it is).

Not sure what you mean by view structure but here is the sql

CREATE VIEW OSNOVNASREDSTVA
AS select "SIFRA","NAZIV","OPIS","STANJE","REVALNABAVNACIJENA","NABAVNACIJENA","AMORTIZACIJA","DATUMNABAVE","SMJENA","STOPAAMORTIZACIJE","ORGANIZACIJA","POCETAKAMORTIZACIJE","ZADNJAAMORTIZACIJA","DEVNABAVNACIJENA","DEVAMORTIZACIJA","ODDATUMA","PREDMET","KONTOZALIHE","KONTOAMORTIZACIJE","GRUPAAMORTIZACIJE","KADAR","LOKACIJA","DOBAVLJAC","PROIZVODJAC","VALUTA","DODATUMA","MJESECI","ID","EAN","IZNOSPROBNEAMORTIZACIJE","DEVIZNOSPROBNEAMORTIZACIJE","DEVIZNOSPROBNEDORAZDOBLJA","IZNOSPROBNEDORAZDOBLJA","DEVREVALNABAVNACIJENA","PRIPREMA","BROJRACUNA","CREATED_BY","MODIFIED_BY","DATE_CREATED","DATE_MODIFIED","BILANCA","PORESKAGRUPAAM","REGISTARSKI_BROJ","MODEL","OSTALO","KOPIRANJE","BROJ_NALOGA","KUF_ID","AMORT_TEKUCA","AMORT_PRIJE_TEK","ZADNJASIFRA" from IBS.os_osnovnasredstva;

Solution

  • Short answer, I was an idiot.

    Long answer the primary key column in the database was of type Varchar2 and I set it as int cos looking at the data it was all numbers so i assumed (making an ass and all that) that it was number without looking. It wasn't. I then changed the class attribute from int to string and it all worked like a charm.