0

Hi all the below solution works in that it creates a record in the MeetingRoomRequest table and also adds the associated amenities to that request into the MeetingRoomRequestAmenityLink table. However it just feels a bit clunky, so I was wondering if there is a nicer solution out there (i.e. not having to create 2 context instances) using MVC 3 and Entity Framework??

Please note i've set up the necessary relationships (one to many) in SQL Server and Entity Framework.

Also please note AmenityList is an array of id's (e.g. [1,2,4])

private readonly IDataRepository<MeetingRoomRequest> _meetingRoomRequestRepository = new DataRepository<MeetingRoomRequest>();
private readonly IDataRepository<MeetingRoomRequestAmenityLink> _meetingRoomRequestAmenityLinkRepository = new DataRepository<MeetingRoomRequestAmenityLink>();

var meetingRoomRequestToAdd = new MeetingRoomRequest
{
    User = meetingRoomRequestViewModel.User,
    UserEmail = meetingRoomRequestViewModel.UserEmail,
    Title = meetingRoomRequestViewModel.Title,
    Comments = meetingRoomRequestViewModel.Comments,
    StartDateTime = meetingRoomRequestViewModel.StartTime,
    EndDateTime = meetingRoomRequestViewModel.EndTime,
    RequestStatusID = (int)Enums.RequestStatus.New,
    AttendeeCount = meetingRoomRequestViewModel.AttendeeCount,
    AttendeeType = meetingRoomRequestViewModel.AttendeeType,
    OfficeID = meetingRoomRequestViewModel.OfficeId,
    LocationID = meetingRoomRequestViewModel.LocationId, 
};
_meetingRoomRequestRepository.Add(meetingRoomRequestToAdd);
_meetingRoomRequestRepository.SaveChanges();
var meetingRoomRequestAdded = meetingRoomRequestToAdd;

foreach (var item in meetingRoomRequestViewModel.AmenityList)
{
    var meetingRoomRequestAmenityLinkToAdd = new MeetingRoomRequestAmenityLink
    {
        AmenityID = item,
        MeetingRoomRequestID = meetingRoomRequestAdded.MeetingRoomRequestID
    };
    _meetingRoomRequestAmenityLinkRepository.Add(meetingRoomRequestAmenityLinkToAdd);
    _meetingRoomRequestAmenityLinkRepository.SaveChanges();
}
4

1 回答 1

1

您的处理方式看起来是正确的,但是在处理请求的效率方面可以进行一些改进。

  1. 由于这些是子/父关系,因此您可以创建父实体,然后在 foreach 循环中附加子实体,然后再调用父实体上的保存更改。EF 将使用父对象的主键(或关联键)自动填充子对象的外键值。
  2. 您可以继续使用您的实体,而无需将其保存回变量。EF 的对象跟踪将在您的整个函数中继续跟踪这一点。
  3. 通过将 savechanges 移到 foreach 循环之外,您可以减少调用次数。我相信在最后一次调用中会发送相同数量的 SQL,但您可能会看到没有打开/关闭连接的增加。EF 可能还有其他内置效率

编码

var meetingRoomRequestToAdd = new MeetingRoomRequest
        {
            User = meetingRoomRequestViewModel.User,
            UserEmail = meetingRoomRequestViewModel.UserEmail,
            Title = meetingRoomRequestViewModel.Title,
            Comments = meetingRoomRequestViewModel.Comments,
            StartDateTime = meetingRoomRequestViewModel.StartTime,
            EndDateTime = meetingRoomRequestViewModel.EndTime,
            RequestStatusID = (int)Enums.RequestStatus.New,
            AttendeeCount = meetingRoomRequestViewModel.AttendeeCount,
            AttendeeType = meetingRoomRequestViewModel.AttendeeType,
            OfficeID = meetingRoomRequestViewModel.OfficeId,
            LocationID = meetingRoomRequestViewModel.LocationId, 
        };
        _meetingRoomRequestRepository.Add(meetingRoomRequestToAdd);

        foreach (var item in meetingRoomRequestViewModel.AmenityList)
        {
            meetingRoomRequestToAdd.MeetingRoomRequestAmenityLinks.Add(new MeetingRoomRequestAmenityLink
            {
                AmenityID = item
            });
        }
        _meetingRoomRequestRepository.SaveChanges();
于 2012-09-28T15:22:42.307 回答