2011-02-02 84 views
0

一個爲0或關係中的表1間的關係這裏是我的課:配置使用功能NHibernate

public class User { 
    public virtual int Id { get; private set;} 
    public virtual IList<Bike> { get; set;} 
} 

public class Bike { 
    public virtual int Id { get; private set;} 
    public virtual string color { get; set;} 
} 

public class Accident{ 
    public virtual int Id { get; private set;} 
    public virtual User User{ get; set;} 
    public virtual Bike Bike{ get; set;} 
} 

而且我的表

  • 用戶:身份證
  • 自行車:ID,顏色,用戶(fk)
  • 意外事故:Id
  • AccdentOccurance:AccidentId,UserId,BikeId

事故可能與自行車(隱式的自行車的用戶),或給用戶,而不是自行車。 我的問題是如何配置的事故類映射與AccidentOccurance表工作?

希望你能幫上忙。

回答

0
Lets see if I can get this right so it is helpful to you. 

1. On the 3rd line, you left out "Bike" for the name you are assigning the 
    relation to. 
2. Lines 4, 10 & 11, you were missing the reverse mappings. 
    I added them with the following assumptions: 
    a. A user can ride more than 1 bike. 
    b. A user can be in more than 1 accident. 
    c. A bike can be involved in more than 1 accident. 
    d. A bike can only be used by 1 user. 
     If you need the bike to be able to be used by more than 1 user 
     change line 10 to: public virtual IList<User> User { get; set;} 
     This will give you a Many to Many relationship. 
3. I would merge the tables Accident and AccidentOccurance into 1 table 
    "Accident". Since if you kept them separated, you are looking at a 1 to 1 
    relationship that does not offer any special benefits. With the vast 
    majority of all 1 to 1 relationships, it is best to merge the tables to 
    limit the SQL queries and speed up your queries on large tables. 


public class User { 
    public virtual int Id { get; private set;} 
    public virtual IList<Bike> Bike { get; set;} // Modified 
    public virtual IList<Accident> Accident { get; set;} // Added 
} 

public class Bike { 
    public virtual int Id { get; private set;} 
    public virtual string color { get; set;} 
    public virtual User User { get; set;} // Added 
    public virtual IList<Accident> Accident { get; set; } // Added 
} 

public class Accident{ 
    public virtual int Id { get; private set;} 
    public virtual User User{ get; set;} 
    public virtual Bike Bike{ get; set;} 
} 
+0

邁克爾喜。 感謝精心答案。 什麼它讓我知道的是,我有一個效率不高表設計,並嘗試將其映射使用NHibernate是一種痛苦,因爲NHibernate的需要邏輯表的設計。我想我會按照你的建議更換桌子。謝謝 – Bugman 2011-02-08 08:13:17