Fluent nHibernate Composite Key of Entities/Beans/Domains

10 views
Skip to first unread message

Ezequiel De Luca

unread,
Sep 27, 2020, 5:33:05 PM9/27/20
to nhusers
Hi:
I am trying to map an object which ID is composed of two Foreign Keys. Below is the Database Scripts
CREATE TABLE tbl_requests
(
     strID uniqueidentifier NOT NULL,
     …..
     CONSTRAINT pk_request PRIMARY KEY (strID),
 )
CREATE TABLE tbl_resources
(
     strCode uniqueidentifier NOT NULL default NEWID(),
     …..
     CONSTRAINT pk_resources PRIMARY KEY (strCode)
)
CREATE TABLE tbl_exceptions
(
     strRequest uniqueidentifier NOT NULL,
     strResource uniqueidentifier NOT NULL
     CONSTRAINT pk_exception PRIMARY KEY (strRequest, strResource),
     CONSTRAINT fk_resourcerequests FOREIGN KEY (strRequest) REFERENCES tbl_requests(strID),
     CONSTRAINT fk_resources FOREIGN KEY (strResource) REFERENCES tbl_resources(strCode)
)
The Entity/Bean/Domain that I created looks like follows:
public class ExceptionEntity
{
     public virtual RequestEntity objRequest { get; set; }
     public virtual ResourceEntity objResource { get; set; }
}
I am having a hard time trying to create the Mapping, so far what I found online were examples of composite key with string properties, but nothing with keys of objects. I was attempting the following,
public ExceptionMap()
{
     Table("tbl_exceptions");
     CompositeId()
          .KeyProperty(x => x.objRequest, "strRequest")
          .KeyProperty(x => x.objResource, "strResource");
}
but I got this exception:

MappingException: Could not determine type for: Project.entities.RequestEntity, Project, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(strRequest)

If I replace key properties with the full path of the Ids ex: .KeyProperty(x => x.objRequest.strID, "strRequest") I get an error that the get method is not defined. Is there any way to achieve this? Should I set 4 properties on my entity 2 string for the keys and 2 objects and make the KeyProperty to the strings and References to the Objects?

Alexander Zaytsev

unread,
Sep 27, 2020, 5:57:24 PM9/27/20
to nhu...@googlegroups.com
You need to use KeyReference instead of KeyProperty. Then it should work.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nhusers/70194a7a-d950-4f66-8155-e743d9d35baen%40googlegroups.com.

madhusudhan rao

unread,
Sep 27, 2020, 8:01:20 PM9/27/20
to nhu...@googlegroups.com
You need to create entitiy classes for every table you are using.


Ezequiel De Luca

unread,
Sep 29, 2020, 4:58:19 PM9/29/20
to nhusers
thanks a lot, It works
Reply all
Reply to author
Forward
0 new messages