更好地将条目添加到主db表和链接表中的方法

时间:2012-09-28 14:45:40

标签: sql-server asp.net-mvc asp.net-mvc-3 entity-framework

以下所有解决方案的工作原理是,它在MeetingRoomRequest表中创建记录,并将相关的设施添加到MeetingRoomRequestAmenityLink表中。然而,它只是感觉有点笨重,所以我想知道是否有一个更好的解决方案(即不必创建2个上下文实例)使用MVC 3和实体框架?

请注意我已经在SQL Server和实体框架中建立了必要的关系(一对多)。

另请注意AmenityList是一个id的数组(例如[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();
}

1 个答案:

答案 0 :(得分:1)

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

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

    守则

    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();