代码之家  ›  专栏  ›  技术社区  ›  Sekhat

流利的NHibernate,与一对多的关系结盟

  •  2
  • Sekhat  · 技术社区  · 15 年前

    好吧,我有两张桌子:

    Companies
        | id int
        | name varchar(50)
    

    Contacts
        | id int
        | name varchar(50)
        | companyID int
    

    在我的代码中,我有以下课程

    public class Company
    {
        public int Identity { get; set; }
        public string Name { get; set; }
        public IList<Contact> Contacts { get; set; }
    }
    

    public class Contact
    {
        public int Identity { get; set; }
        public string Name { get; set; }
        public Company Company { get; set; }
    }
    

    我流畅的NHibernate映射如下:

    public class CompanyMapping : ClassMap<Company>
    {
        public CompanyMapping()
        {
            WithTable("Companies");
    
            Id(x => x.Identity, "Id");
            Map(x => x.Name);            
            HasMany<Contact>(x => x.Contacts)                                                
                .Inverse()                
                .LazyLoad()                
                .Cascade.All()
                .AsList();                
        }
    }
    

    public class ContactMapping : ClassMap<Contact>
    {
        public ContactMapping()
        {
            WithTable("Contacts");
    
            Id(x => x.Identity, "Id");
            References<Company>(x => x.Company, "CompanyID");
            Map(x => x.Name);
        }
    }
    

    但是,当我尝试访问company.contacts属性时,会得到以下错误

    Invalid column name 'Company_id'.
    Invalid column name 'Company_id'.
    

    (一条消息中有两次)

    显然,contacts表上的键列不是company-id,而是company id

    我做错了什么?我似乎无法将键列设置为WithKeyColumn似乎不存在的内容(这是我在其他解决方案中发现的,但他们可能使用的是其他版本的Fluent NHibernate)

    提前谢谢

    1 回复  |  直到 15 年前
        1
  •  4
  •   Community CDub    8 年前

    Fluent NHibernate - HasMany().WithKeyColumnName

    编辑:

    显然,因为我今天已经接受了我自己对另一个问题的回答,我必须再等两天。

    那种很烂,但是我。