简体   繁体   English

我需要定义这两个实体之间的任何关系吗?

[英]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. VacationReservationHotelRoomReview ,自定义IdentityUserRecordUserRecord用于更好地管理创建和修改日期和用户。

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 ?我的问题是,我应该定义ReservationRoom之间的任何关系吗?

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的实例已经存在于HotelList<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.

相关问题 我要在依赖项或委托人上的两个实体之间定义关系吗? - Do I define a relationship between two entities on the dependent or the principal? 在 Entity Framework Core 中,如果我定义这些关系,它会在两个实体之间创建循环关系吗? - In Entity Framework Core, if I define these relationships, will it create a cyclic relationship between two entities? 两个实体之间的可选关系 - Optional relationship between two entities 我需要在两个代码优先的实体之间创建一个复杂的关系,但是我很难缠着它 - I need to create a complex relationship between two code-first entities but I'm having a hard time wrapping my brain around it 如何映射这两个实体之间的关系 - How to map the relationship between these two entities 如何定义两个实体之间的多个关系? - How to define multiple relationships between two entities? 我是否需要使用EntityFramework更新一对多关系中的两个实体? - Do I need to update both Entities in a one-to-many relationship using EntityFramework? 我是否需要断开连接的实体? - Do I need disconnected entities? 无法在同一两个实体之间定义两个关系 - Can't define two relationships between the same two entities 如何解决一对多关系,其中一侧有两个相同类型的实体 - How do I fix one to many relationship where one side has two entities of the same type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM