4

我的项目涉及创建一个新的酒店房间,我的数据库中的 2 个表将更新。我的表称为 RoomType 和 RoomFacility。

我可以成功更新 RoomType,但是当我尝试更新 RoomFacility 并使用 RoomTypeID 创建新的房间设施时,它失败了。我的 RoomFacilityID 总是得到 1。

如何更新两个表、roomType 和 RoomFacility 的数据?

这是我的服务更新我的数据库的代码

 public void UpdateFacilityInRooms(List<int> FacilityIDs, int RoomTypeID)
    {
        List<HotelRoomFacility> hotelRoomFacilities =
                            _HotelRoomFacilityRopository.AsQueryable()
                            .Where(f => f.RoomTypeID == RoomTypeID).ToList();

        foreach (int newRoomFacility in FacilityIDs)
        {
            if (hotelRoomFacilities.Where(h => h.RoomFacilityID == newRoomFacility).Count() == 0)
            {
                HotelRoomFacility facility = new HotelRoomFacility
                {
                    RoomFacilityID = newRoomFacility,
                    RoomTypeID = RoomTypeID
                };
                _HotelRoomFacilityRopository.Add(facility);
            }
        }
        _HotelRoomFacilityRopository.CommitChanges();
    }




 public RoomType NewRoom(int HotelID,int? RoomTypeID, 
                        string RoomTypeName, string RoomTypeDescription)
    {
        RoomType room = new RoomType();
        room.HotelID = HotelID;
        room.RoomTypeID = RoomTypeID ?? 0;
        room.RoomtypeName = RoomTypeName;
        room.RoomTypeDescripton = RoomTypeDescription;

        _RoomTypeRepository.Add(room);

        _RoomTypeRepository.CommitChanges();

        return room;
      }
4

1 回答 1

0
 public RoomType UpdateRoom(int RoomTypeID, string RoomTypeName, string RoomTypeDescription, List<int> RoomFacilityIDs)
        {

            RoomType roomType = (from rt in _RoomTypeRepository.AsQueryable().Include(r => r.HotelRoomFacilities)
                                 where rt.RoomTypeID == RoomTypeID
                                select rt).FirstOrDefault();

            if (roomType == null)
                return null;

            roomType.RoomTypeName = RoomTypeName;
            roomType.RoomTypeDescripton = RoomTypeDescription;

            //Add New Room facilities
            List<HotelRoomFacility> hotelRoomFacilities = _HotelRoomFacilityRopository.AsQueryable().Where(f => f.RoomTypeID == RoomTypeID).ToList();
            foreach (int newRoomFacilityID in RoomFacilityIDs)
            {
                if (roomType.HotelRoomFacilities.Where(h => h.RoomFacilityID == newRoomFacilityID).Count() == 0)
                {
                    roomType.HotelRoomFacilities.Add(new HotelRoomFacility
                    {
                        RoomFacilityID = newRoomFacilityID
                    });
                }
            }


            foreach (HotelRoomFacility roomFacility in hotelRoomFacilities)
            {
                if (RoomFacilityIDs.Contains(roomFacility.RoomFacilityID) == false)
                    _HotelRoomFacilityRopository.Delete(roomFacility);
            }

            _RoomTypeRepository.Attach(roomType);
            _RoomTypeRepository.CommitChanges();

            return roomType;
        }
于 2013-07-11T01:45:32.480 回答