2010-09-16 5 views
8

Używam Fluent NHibernate i mam dwie tabele:Dołącz do tabeli za pomocą dwóch kolumn non-FK z Fluent NHibernate

BusinessPlan [Id, Year, CustomerCode] 

PreviousYearData [Id, Year, CustomerCode, MoreFieldsForData] 

w mojej domenie, chcę dołączyć PreviousYearData do biznes planu, aby podmioty coś takiego:

public class BusinessPlan { 
    public Guid Id { get; set; } 
    public int Year { get; set; } 
    public string CustomerCode { get; set; } 
    public PreviousYearData PreviousYearData {get; set;} 
} 

public class PreviousYearData { 
    public Guid Id { get; set; } 
    public int Year { get; set; } 
    public string CustomerCode { get; set; } 
    // many more fields 
} 

dane w tabeli PreviousYearData zostanie oznakowane na początku tego roku, zanim zostaną utworzone BusinessPlans, więc nie będzie wiedział, co będzie biznes planu identyfikatora i nie można utworzyć normalny klucz obcy. Co myślę, że chcę zrobić, to dołączyć do PreviousYearData do BusinessPlan na podstawie dwóch kolumn Year i CustomerCode. Czy to możliwe dzięki Fluent NHibernate? Czy istnieje inny sposób podejścia do tego, który ma więcej sensu?

Odpowiedz

1

Chyba ten lub podobny powinny pracować dla Ciebie:

 HasMany(x => x.PreviousYearDatas) 
      .Access.AsCamelCaseField(Prefix.Underscore) 
      .WithKeyColumns("YEAR", "CUSTOMER_CODE") 
      .LazyLoad(); 

Będziesz mieć kolekcję, ale będzie można dostać to, czego chcesz.

Ponadto istnieje inna konstrukcja:

 HasMany( 
      // some mapping but includes one foreign key - say customer code 
      .Where("YEAR = 2010") 

Pewnie, że to najlepszy wybór. Nie sądzę, że rok zmieniają się często :)

0

nie widzę dlaczego nie ma problemu z posiadaniem klucz obcy w tabeli biznes planu: PreviousYearDataId

w mapowaniu biznes planu tylko dodać:

References(x => x.PreviousYearData) 
    .Column("PreviousYearDataId") 
+0

Tak. Jeśli tabela FirstYearData jest wypełniona jako pierwsza, dlaczego tabela BusinessPlan nie ma identyfikatora odpowiadającego jej rekordu PreviousYearData? – David

Powiązane problemy