34

I am newbie to NHibernate and trying to use Fluent for mapping. My entity class name is different from the database table name it has to be mapped to. I am using mapping class derived from ClassMap<>, but I can't specify the table name: the property TableName from ClassMap is read-only.

Thanks for your help.

AlG
  • 14,697
  • 4
  • 41
  • 54
twk
  • 3,122
  • 2
  • 26
  • 36

3 Answers3

45

Use Table("table_name") instead.

vgru
  • 49,838
  • 16
  • 120
  • 201
Spikolynn
  • 4,067
  • 2
  • 37
  • 44
6

Does it work like?

public class UserAccountMap : ClassMap<UserAccount> {
    public UserAccountMap() {
        Table("User");
        Id(x => x.UserID, "UserID").GeneratedBy.Identity();
        Map(x => x.FirstName, "FirstName").Length(50)
            .Not.Nullable();
        Map(x => x.LastName, "LastName").Length(50)
            .Not.Nullable();
    }
}
hardywang
  • 4,864
  • 11
  • 65
  • 101
  • 1
    User is a reserved keyword. enclose with `. See http://stackoverflow.com/a/2879630/475882 – jaxxbo Mar 15 '14 at 18:44
6

The example above lead me in the right direction. This worked for me. My class name is "Party" and my table name is "prty.Party".

public class PartyMap : ClassMap<Party>
{
    public PartyMap()
    {
     Table("prty.Party");
     Id(x => x.PartyID);
     Map(x => x.PartyTypeLID);
     Map(x => x.OrganizationTypeLID);
     Map(x => x.PreferredContactMethodLID);
     Map(x => x.PrimaryLanguageLID);
     Map(x => x.PartyVID);
     Map(x => x.BeginDate);
     Map(x => x.EndDate);
     Map(x => x.RowDescriptor);
     Map(x => x.RowModifiedDate);
     Map(x => x.RowModifiedBy);
     Map(x => x.RowCreatedDate);
     Map(x => x.RowCreatedBy);
    }        
}