Foreign key part of unique constraint or primary key

Original Post Fionn's Avatar

Fionn

24 May, 2010 09:05 PM via web

Hi,

is it possible to include the foreign-key which is created for an many-to-one mapping in an index/unique constraint with a fluent mapping?

I have something like the following:

class Document
{
    Guid Id;
    public virtual IList<DocumentRevision> Revisions { get; private set; }
}

class DocumentRevision
{
    Guid document_id; 
    int revision;
}

Is it possible to have a primary key for the document revision table which consists of document_id and revision, since a given revision must only exist a single time for each document. Also a combined index should be beneficial because most querys would be like give me the latest revision of a given document.

But I haven't found a way to create a mapping for this.

Thanks

  1. 2 Posted by rkevinstout on 25 May, 2010 08:58 PM

    rkevinstout's Avatar

    This can be accomplished by using a CompositeId. For example:

        class DocumentRevisionMap : ClassMap<DocumentRevision>
        {
            public DocumentRevisionMap()
            {
                CompositeId()
                    .KeyProperty(x => x.revision)
                    .KeyReference(x => x.document_id);  
            }
        }
    

    Good luck,

    Kevin Stout

  2. 3 Posted by Fionn on 25 May, 2010 09:47 PM

    Fionn's Avatar

    Would it also be possible if not specifying the foreign key as Guid but as class reference, like with this classes:

    class Document
    {
        Guid Id;
        public virtual IList<DocumentRevision> Revisions { get; private set; }
    }
    
    class DocumentRevision
    {
        Document ParentDocument;
        int Revision;
    }
    

    Thanks

  3. 4 Posted by rkevinstout on 25 May, 2010 10:42 PM

    rkevinstout's Avatar

    Yes, you are correct.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is the opposite of bad?

    Recent Discussions

    05 Jul, 2010 10:29 PM
    05 Jul, 2010 12:45 PM
    05 Jul, 2010 12:42 PM
    05 Jul, 2010 12:17 PM
    05 Jul, 2010 12:12 PM

     

    03 Jul, 2010 12:26 AM
    02 Jul, 2010 02:17 PM
    02 Jul, 2010 08:18 AM
    02 Jul, 2010 12:20 AM
    01 Jul, 2010 10:14 PM