简体   繁体   中英

Fluent Nhibernate, stuggling with one-to-many relationships

Okay so I have two tables:

Companies
    | id int
    | name varchar(50)

and

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

In my code I have the following classes

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

and

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

And my fluent nhibernate mappings as so:

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

and

public class ContactMapping : ClassMap<Contact>
{
    public ContactMapping()
    {
        WithTable("Contacts");

        Id(x => x.Identity, "Id");
        References<Company>(x => x.Company, "CompanyID");
        Map(x => x.Name);
    }
}

Yet when I try to access the Company.Contacts property I get the following error

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

(yes twice in one message)

obviously the key column on the contacts table isn't called Company_id it's called CompanyID

So what am I doing wrong? I can't seem to set what the Key Column as WithKeyColumn doesn't seem to exist (it's what I've found in other solutions people have done, but they might be using a different version of fluent nhibernate to me)

Thanks in advance

Fluent NHibernate - HasMany().WithKeyColumnName

solved it :)

Edit:

Apparently, because I've accepted my own answer on another question today, I have to wait another two days to do so.

That kind sucks but meh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM