2011-02-12 35 views
2

我有这样的事情如何判断流利休眠如何命名外键?

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); 
     } 
    } 

正如你可以看到我试着告诉AppointmentReminderMap什么FK的名称是试图ForiegnKey和列然而,当我得到这个错误

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: 

它找为CalendarAppointmentId。我不是为什么它重复两次。所以我让流利的nhibernate生成我的数据库来看看发生了什么。当我查看约会提醒表时,它具有CalendarAppointmentId的fk。

为什么它不使用我指定的名称?

这里是我的配置

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); 
     } 

回答

2

尝试:

HasMany(x => x.AppointmentReminders).KeyColumn("AppointmentId"); 
1

ForeignKey的是FK约束的名称,而不是列。您可能需要确保HasMany使用相同的列名称...“AppointmentId”。您使用的约定是将其默认为CalendarAppointmentId,这与您在一对多方指定的内容相冲突。所以..另一种选择是删除Column("AppointmentId")一对多,让大会做这件事。

+0

雅那我怎么过的最初但没有奏效。然后我尝试了.Column()然后我添加了.ForeignKey()来查看两者是否都能工作 – chobo2 2011-02-12 06:09:00

+0

@ chobo2你对你的upvotes很吝啬吧? – dotjoe 2011-02-12 20:17:00