"This key was not in the dictionary" nHibernate C #

I am new to nHibernate. I set up the following one-to-one mapping between the two User and User File tables.

User.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Core.Domain.Model" assembly="Core"> <class name="User" table="Users" dynamic-update="true" lazy="false"> <cache usage="read-write"/> <id name="UserId" column="UserId" type="guid"> </id> <one-to-one name="UserProfile" class="UserProfile"/> </class> </hibernate-mapping> 

UserProfile.hbm.xml:

 <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Core.Domain.Model" assembly="Core"> <class name="UserProfile" table="UserProfiles" dynamic-update="true" lazy="false"> <cache usage="read-write"/> <id name="UserProfileId" column="UserProfileId" type="int"> <generator class="native"/> </id> <property name="Description" length="100"/> <many-to-one name="User" unique="true" column="UserId"/> </class> </hibernate-mapping> 

My POCOs for the above mappings:

 public class User { public virtual Guid UserId { get; set; } public virtual UserProfile UserProfile { get; set; } } public class UserProfile { public virtual int UserProfileId { get; set; } public virtual User User { get; set; } } 

Now, when I try to save my User object, I get an exception: " The specified key is not in the dictionary in the following line:

 using (ISession session = SessionFactory.OpenSession()) 

Does anyone know what could be happening here?

+4
source share
3 answers

I copied your hbm mappings and your classes into my test project. I got an error while creating a SessionFactory. Is there any chance that you will create a SessionFactory the first time you access it in your use-statement? If so, this will hopefully solve the problem:

I fixed it by adding

 public virtual String Description { get; set; } 

to the UserProfile class. If you have this in your code and just forgot that during copy and paste I will try to continue the study.

Edit:

Another possible source of this error is as found here :

Make sure all your .hbm.xml files are embedded resources.

If this still does not help, could you please put StackTrace for an exception?

+4
source

The error is ambiguous.

In my case, this was due to a dynamically generated HQL statement with a missing HQL where clause, although it had conditions.

That is, the WRONGLY generated HQL was something like

 from User u left join u.Profile (id = :id) 

Somewhere in the query chain, where clause was missing. To become:

 from User u left join u.Profile where (id = :id) 
0
source

There was the same problem! For me it was the same as Kerzek said, from one to one link with different column names. I added constrained = "false" to the one-to-one mapping:

 <one-to-one name="Person" class="PersonClass" cascade="none" constrained="false" fetch="join"/> 
0
source

Source: https://habr.com/ru/post/1343668/


All Articles