gpt4 book ai didi

sql-server - 如何告诉 Fluent hibernate 命名外键的内容?

转载 作者:行者123 更新时间:2023-12-04 06:35:01 25 4
gpt4 key购买 nike

我有这样的事情

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

如您所见,当我收到此错误时,我尝试通过尝试 ForiegnKey 和 Column 来告诉 AppointmentReminderMap fk 的名称是什么
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。我不明白为什么它重复两次。所以我让 fluent 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);
}

最佳答案

尝试:

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

关于sql-server - 如何告诉 Fluent hibernate 命名外键的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com