FluentNhibernate mapping when there are two primary keys

I am using Fluent Nhibernate for my C # application and I have a very simple question. I would like to know what the NHibernate mapping class looks like when we have multiple primary keys.

Here is an example showing how I implement mapping and other model classes:

CREATE TABLE Students(
   id   INT              NOT NULL,
   name VARCHAR (20)     NOT NULL,
   age  INT              NOT NULL,       
   PRIMARY KEY (id)
);

      


public class Students
{
   public virtual int      ID   { get; set; }
   public virtual string   Name { get; set; }
   public virtual int      Age  { get; set; }
}


public class StudentMap : ClassMap<Students>
{
    public AchivementMasterMap()
    {
       Id(x => x.ID).Column("id");
       Map(x => x.Name).Column("name");
       Map(x => x.Age).Column("age");
       Table("Students");
     }
}

      

Suppose we have two primary keys on Student table such as

CREATE TABLE Students(
       id   INT              NOT NULL,
       name VARCHAR (20)     NOT NULL,
       age  INT              NOT NULL,       
       PRIMARY KEY (id,name)
);

      

So how do I write the display class for the above table?

+3


source to share


1 answer


You don't have two primary keys, you have a composite primary key. I think you can use Fluent NHibernate CompositeId - something like this:



public class StudentMap : ClassMap<Students>
{
    public StudentMap()
    {
            CompositeId()
                .KeyProperty(x => x.name, "id")
                .KeyProperty(x => x.name, "name");

            ...

      

+1


source







All Articles