Note that there are some explanatory texts on larger screens.

plurals
  1. POForeign Key to a table with 2 columns primary key (CompositeId)
    primarykey
    data
    text
    <p>I've defined the primary key as following:</p> <pre><code>CompositeId() .KeyProperty(x =&gt; x.Id) .KeyProperty(x =&gt; x.Type); </code></pre> <p>I've tried the following:</p> <pre><code>References(x =&gt; x.EntityWith2ColsPK); </code></pre> <p>And failed with: </p> <blockquote> <p>Foreign key (Fk_MyEntity_EntityWith2ColsPK:MyEntities [Fk_EntityWith2ColsPK])) must have same number of columns as the referenced primary key (EntityWith2ColsPKs [Id, Type])</p> </blockquote> <p>How can I reference EntityWith2ColsPK from another entity?</p> <hr> <p><strong>Update:</strong></p> <p>I've tried the following (according to AlfeG's comment):</p> <pre><code>HasMany&lt;EntityWith2ColsPK&gt;(x =&gt; x.EntityWith2ColsPK).KeyColumns.Add("Id", "Type").Cascade.All(); </code></pre> <p>Which failed with:</p> <blockquote> <p>Custom type does not implement UserCollectionType: EntityWith2ColsPK</p> </blockquote> <p>But anyway I don't want a 1 to many relation, I want a 1 to 1 relation. Still, I can't make either of them work.</p> <p>Also, I've tried:</p> <pre><code>HasOne&lt;EntityWith2ColsPK&gt;(x =&gt; x.EntityWith2ColsPK).PropertyRef(x =&gt; x.Id).PropertyRef(x =&gt; x.Type); </code></pre> <p>Which fails with:</p> <blockquote> <p>NHibernate.MappingException : property not found: Type on entity EntityWith2ColsPK</p> </blockquote> <p>What can I do for this to really work?</p> <hr> <p>I managed to achieve something in the db.. but yet, for some reason I suspect it maps the property "Type" twice, because I want it to be both part of the Primary Key, and part of the Foreign Key.. This is what I did:</p> <pre><code>References(x =&gt; x.EntityWith2ColsPK).Columns("EntityWith2ColsPKId", "Type").Formula("Id = :EntityWith2ColsPKId AND Type = :Type"); </code></pre> <p>But I received the following exception:</p> <blockquote> <p>System.IndexOutOfRangeException : Invalid index 8 for this SqlParameterCollection with Count=8.</p> </blockquote> <p>Because the mapping of this entity is same as EntityWith2ColsPK:</p> <pre><code>CompositeId() .KeyProperty(x =&gt; x.Id) .KeyProperty(x =&gt; ((ILocalizedEntity) x).Language); </code></pre> <p>HELP!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload