使用 Fluent nhibernate 在关系表中配置一对 0 或 1 关系

发布于 2024-10-15 04:55:33 字数 681 浏览 1 评论 0原文

这是我的课程:

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
  • 自行车:Id、颜色、用户(fk)
  • 事故:Id
  • AccdentOccurance:AccidentId、UserId、BikeId

事故可能与自行车相关(隐式与自行车的用户相关),或者给用户,但不是自行车。 我的问题是如何配置 Accident ClassMap 以与 AccidentOccurance 表一起使用?

希望你能帮忙。

Here are my classes:

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

And my tables

  • User: Id
  • Bike: Id, color, user(fk)
  • Accident: Id
  • AccdentOccurance: AccidentId, UserId, BikeId

An accident can be related to a bike(implicit to the user of the bike), or to a user, but not a bike.
My question is how do I configure the Accident ClassMap to work with the AccidentOccurance table?

Hope you can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-10-22 04:55:33
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;}
}
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;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文