开发者

Fluent nHibernate - Wrong column name when self reference

开发者 https://www.devze.com 2023-04-10 20:44 出处:网络
The mapping on foreign key are made with the wrong name. Why? Here\'s my classes: The order of the properties seems to be important:

The mapping on foreign key are made with the wrong name. Why?

Here's my classes:

The order of the properties seems to be important:

public class Person
{
  public virtual Person Mother { get; set; }
  public virtual IList<Item> Items { get; set; }
  public virtual Person Father { get; set; }
}
public class Item
{
  public virtual string Name { get; set; }
}

Here'开发者_开发问答s the mapping with Fluent Nhibernate

AutoMap.AssemblyOf<Person>(new CustomAutomappingConfiguration())

When I look to the database, the foreign key in the table seems to be the name of the first property with the type Person after the property Items. Here's the SQL generated to create the table:

CREATE TABLE "Item" (Id  integer primary key autoincrement
    , Name TEXT
    , Father_id BIGINT
    , constraint FKC57C4A2B4586680 foreign key (Father_id) references Patient)

Thank you in advance for your help ;)


The solution I've found is to override the configuraton like this:

AutoMap.AssemblyOf<Person>(new CustomAutomappingConfiguration())
    .Override<Person>(m => m.HasMany<Item>(x => x.Items).KeyColumn("Patient_Id"))

Does exist any solution to let the auto mapping work seamlessly? And how Fluent nHibernate works to choose the name of the foreign key's column?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号