Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:16:55+00:00 2026-06-01T12:16:55+00:00

I’m facing a apparently very strange problem (I must be doing something wrong, just

  • 0

I’m facing a apparently very strange problem (I must be doing something wrong, just can’t find my errors!). When certain POCOs are saved into the database, nothing happens. When the same POCOs have some property changed, I get a InvalidCastException during session flush, and the rows are never updated. Here’s the details:

I have the following class declared:

namespace Data
{
    public class Picture
    {
        public virtual int picid { get; set; }
        public virtual int width { get; set; }
        public virtual int height { get; set; }
        public virtual string path { get; set; }
        public virtual string thumbnail { get; set; }
        public virtual int userid { get; set; }
        public virtual int? placeid { get; set; }
        public virtual int? eventid { get; set; }
        public virtual DateTime? approved { get; set; }
        public virtual DateTime date { get; set; }
        public virtual bool finished { get; set; }

        public virtual User User { get; set; }
        public virtual Place Place { get; set; }
        public virtual Event Event { get; set; }

        public virtual ISet<PictureVote> Votes { get; set; }
    }

}

and the following mapping for it:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Data" namespace="Data">
  <class name="Picture" table="pictures">
    <id name="picid">
        <generator class="sequence">
            <param name="sequence">pictures_picid_seq</param>
        </generator>
    </id>
    <property name="path" />
    <property name="width" />
    <property name="height" />
    <property name="thumbnail" />
    <property name="userid" />
    <property name="placeid" not-null="false" />
    <property name="eventid" not-null="false" />
    <property name="approved" />
    <property name="date" />
    <property name="finished" />

    <many-to-one name="User" column="userid" class="Data.User,Data" insert="false" />
    <many-to-one name="Place" column="placeid" class="Data.Place,Data" insert="false" />
    <many-to-one name="Event" column="eventid" class="Data.Event,Data" insert="false" />
    <set name="Votes">
        <key column="picid" />
        <one-to-many class="PictureVote" />
    </set>
  </class>

</hibernate-mapping>

I checked the table definition and all the types seem to be according to the defined class (postgresql):

                                      Table "public.pictures"
  Column   |            Type             |                        Modifiers                        
-----------+-----------------------------+----------------------------------------------------------
 picid     | integer                     | not null default nextval('pictures_picid_seq'::regclass)
 path      | character varying(250)      | not null
 thumbnail | character varying(250)      | not null
 userid    | integer                     | not null
 placeid   | integer                     |
 date      | timestamp without time zone | not null
 finished  | boolean                     | not null default false
 width     | integer                     | not null
 height    | integer                     | not null
 eventid   | integer                     |
 approved  | timestamp without time zone |

When inside the code, the following works just fine and inserts a row in the pictures table:
…
…

var plpic = new Picture
                {
                   date = DateTime.Now,
                   width = img.Width,
                   height = img.Height,
                   path = pic_server_path,
                   thumbnail = thumb_server_path,
                   userid = CustomUserManagement.User.userid,
                   finished = false,
                   approved = null,
                   placeid = placeid
                };
                session.Save(plpic);
                session.Flush ();

…
…
which works ok every time (and yes, I’m going to wrap it in a transaction soon enough).

However, later on, the following NEVER works (this is the code inside a MVC action):
…
…

    Picture pic = session.Get<Picture>(picid);
            // While debugging, I verified that the "pic" object above is retrieved just fine.
            if (pic.userid != CustomUserManagement.User.userid || ModelState.IsValid == false)
                return Json (new { status = "error" });

            using (ITransaction tx = session.BeginTransaction()) {
                try
            {
                pic.finished = true;

                tx.Commit();
            }
            catch (Exception e) {
                tx.Rollback();
                NHibernateHelper.DestroySession();

                return Json (new { status = "error" });
            }
        }

…
…
But this always throws a System.InvalidCastException: Cannot cast from source type to destination type at NpgsqlTypes.NpgsqlTypesHelper+c_Iterator11.<>m_C (System.Object timestamp) [0x00000] in /Users/fxjr/Desenvolvimento/ProjetosOpenSource/Npgsql/NpgsqlSourceRelease/Npgsql2/src/NpgsqlTypes/NpgsqlTypesHelper.cs:608

What am I doing wrong? I’m using .NET 4 in Mono 2.10.5, even though the same happens on Windows, NHibernate 3.2 and Npgsql 2.0.11.91. I’m also using postgresql 9.1.1, but I have set ansi_conforming_strings to OFF to make sure my Nhibernate dialect still works. For more information, the same thing happens when updating other types of objects.

I have already posted this in the nhusers list and received a very good suggestion on trying a different db to check if it its Npgsql’s fault. However, I don’t have the time for that right now and since I’m beginning to think it is my fault and not someone else’s, I thought I’d post this here before recreating my db schema in another database and trying this code in it. I’m getting a little desperate as time is kind of running out on me.. can anyone save me on this one?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:16:56+00:00Added an answer on June 1, 2026 at 12:16 pm

    After a long time, I thought I should come back here to answer my own question.
    This is not necessarily a bug in either NHibernate or Npgsql.
    I was making a huge mistake in the mapping files, by mapping the same column twice. What this means is that having a int? eventid and a Event Event in my mapped class both map to same column will create you problems, since NHibernate can’t deal with this, i.e. you have to pick one of them. What happens is that NHibernate will generate queries with more parameters than the actual number of columns in your table. In MS SQL Server, it is easier to understand the thrown exception:

    Invalid index N for this SqlParameterCollection with Count=N

    Another alternative is mapping both but setting insert=”false” and update=”false” in one of them, although this is hardly useful.
    I think a lot of people like me (coming from LINQ-TO-SQL) will try the same feat and have a lot of trouble later on. Sadly, I haven’t found anything that emphasizes this in NHibernate’s documentation.

    Hope it helps someone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.