2

I have something like this

 public class AppointmentReminder
    {
        public virtual int ReminderId { get; private set; }
        public virtual CalendarAppointment CalendarAppointment { get; set; }
    }



   public class CalendarAppointment
    {
        public virtual int AppointmentId { get; private set; }
        public virtual IList<AppointmentReminder> AppointmentReminders { get; set; }


        public CalendarAppointment()
        {
            AppointmentReminders = new List<AppointmentReminder>();
        }
    }

   public class AppointmentReminderMap : ClassMap<AppointmentReminder>
    {
        public AppointmentReminderMap()
        {
            Table("AppointmentReminders");
            Id(x => x.ReminderId);
            References(x => x.CalendarAppointment).ForeignKey("AppointmentId").Column("AppointmentId").Not.Nullable();
        }
    }

  public class CalendarAppointmentMap : ClassMap<CalendarAppointment>
    {
        public CalendarAppointmentMap()
        {
            Table("CalendarAppointments");
            Id(x => x.AppointmentId);
            HasMany(x => x.AppointmentReminders);
        }
    }

As you can see I try to tell AppointmentReminderMap what the name of the fk is by trying ForiegnKey and Column yet when I get this error

Server Error in '/' Application.
Invalid column name 'CalendarAppointmentId'.
Invalid column name 'CalendarAppointmentId'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'CalendarAppointmentId'.
Invalid column name 'CalendarAppointmentId'.

Source Error:

It looking for CalendarAppointmentId. I don't why it repeats it twice. So I let fluent nhibernate generate my database to see what was going on. When I look at the appointmentReminder table it has a fk of CalendarAppointmentId.

Why does it not use the name that I specified?

Here is my config

   public ISessionFactory GetSessionFactory()
        {
           ISessionFactory fluentConfiguration = Fluently.Configure()
                                                  .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("ConnectionString")))
                                                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Framework.Data.Mapping.MyMap>().Conventions.Add(ForeignKey.EndsWith("Id")))
                                                  //.ExposeConfiguration(BuidSchema)
                                                  .BuildSessionFactory();

            return fluentConfiguration;
        }

        private static void BuidSchema(NHibernate.Cfg.Configuration config)
        {
            new NHibernate.Tool.hbm2ddl.SchemaExport(config).Create(false, true);
        }
chobo2
  • 83,322
  • 195
  • 530
  • 832

2 Answers2

2

Try:

HasMany(x => x.AppointmentReminders).KeyColumn("AppointmentId");
Michael Buen
  • 38,643
  • 9
  • 94
  • 118
  • That seems to work if I team it up with .Column() on the Reference one. – chobo2 Feb 12 '11 at 06:09
  • Yeah, must be done both on both ends, that compelled me to write a helper object, so .KeyColumn and .Column won't needed be specified. But my helper object doesn't work on entities and mappings that are in different project http://www.ienablemuch.com/2010/12/brownfield-system-problem-on-fluent.html However, writing that helper object gained me a lot of insights on how Fluent NHibernate works :-) And another attempt: http://code.google.com/p/fluent-nhibernate-lowercase-system/ – Michael Buen Feb 12 '11 at 06:58
1

ForeignKey is the name of the fk constraint, not the column. You probably need to make sure the HasMany is using the same column name..."AppointmentId". That convention you're using is making it default to CalendarAppointmentId which conflicts with what you've specified on the one-to-many side. So..another option would be to remove the Column("AppointmentId") on the one-to-many and let the convention do it's thing.

dotjoe
  • 26,242
  • 5
  • 63
  • 77
  • Ya that's how I had it originally but that did not work. I then tried just .Column() then I added .ForeignKey() to see if having both would work – chobo2 Feb 12 '11 at 06:09
  • Lol naw I just usually forget. I usually forget to mark something as expected as well. So every month or 2 when I have a couple spare moments I sit down and go through my posts and give up votes and accepted answers. – chobo2 Feb 12 '11 at 20:39