[英]Do I need to define any relationship between these two entities?
Hello everyone and sorry for this noob question.大家好,很抱歉这个菜鸟问题。 I'm currently developing an ASP.NET Core 3.1 WebAPI for a Travel Planner & Assistant web application.
我目前正在为旅行计划者和助手 web 应用程序开发 ASP.NET Core 3.1 WebAPI。 I am using EF Core and Identity.
我正在使用 EF Core 和 Identity。
My model consists of the following classes:我的 model 包含以下类:
Vacation
, Reservation
, Hotel
, Room
, Review
, a custom IdentityUser
and Record
, UserRecord
for a better management of the creation and modification date and user. Vacation
, Reservation
, Hotel
, Room
, Review
,自定义IdentityUser
和Record
, UserRecord
用于更好地管理创建和修改日期和用户。
Each Vacation
has a List<Reservation>
;每个
Vacation
都有一个List<Reservation>
; each Hotel
has a List<Room>
and a List<Review>
.每个
Hotel
都有一个List<Room>
和一个List<Review>
。
My question is, should I define any relationship between Reservation
and Room
?我的问题是,我应该定义
Reservation
和Room
之间的任何关系吗?
I'm thinking each Reservation
should know which Room
is going to book, so it seems logical to have the Room
inside.我认为每个
Reservation
都应该知道要预订哪个Room
,因此将Room
放在里面似乎是合乎逻辑的。 But that instance of Room
already exists in the List<Room>
of the Hotel
.但是
Room
的实例已经存在于Hotel
的List<Room>
中。
I'm thinking each Reservation should know which Room is going to book, so it seems logical to have the Room inside.
我认为每个预订都应该知道要预订哪个房间,因此将房间放在里面似乎是合乎逻辑的。 But that instance of Room already exists in the List of the Hotel.
但是那个房间的实例已经存在于酒店的列表中。
What you tought is totally correct.你所坚持的是完全正确的。 You don't need to add a collection naivgational property of type
Reservation
(eg List<Reservation>
) into your Room
entity.您不需要将
Reservation
类型的集合导航属性(例如List<Reservation>
)添加到您的Room
实体中。
By adding a Room
navigational property on Reservation
entity, EF Core can handle the remainging things and by applying default convention it will consider that a reservation is for one room and a room can be related to multiple reservations even you don't created the reservation type into the Room
entity.通过在
Reservation
实体上添加Room
导航属性,EF Core 可以处理剩余的事情,并且通过应用默认约定,它会认为预订是针对一个房间的,即使您没有创建预订类型,一个房间也可以与多个预订相关联进入Room
实体。
What I think, you should have a new entity HotelRoom, which contains Hotel and Room, many to many relation between Hotel and Room.我认为,您应该有一个新的实体 HotelRoom,其中包含 Hotel 和 Room,Hotel 和 Room 之间的多对多关系。 Then for every Reservation you should have Hotel and a list of HotelRoom in it.
然后,对于每个预订,您都应该在其中包含 Hotel 和 HotelRoom 列表。
public class Hotel
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Room
{
public int Id { get; set; }
public int RoomNumber { get; set; }
}
public class HotelRoom
{
public int HotelId { get; set; }
public Hotel Hotel { get; set; }
public int RoomId { get; set; }
public Room Room { get; set; }
}
public class Reservation
{
public int Id { get; set; }
public int HotelId { get; set; }
public Hotel Hotel { get; set; }
public List<HotelRoom> HotelRooms { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.