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.Configur开发者_如何学运维e()
.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);
}
Try:
HasMany(x => x.AppointmentReminders).KeyColumn("AppointmentId");
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.
精彩评论