Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:34:01+00:00 2026-06-12T04:34:01+00:00

Hi all the below solution works in that it creates a record in the

  • 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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:34:03+00:00Added an answer on June 12, 2026 at 4:34 am

    The way you are going about it looks right, but there are some improvements that could be made in efficiency of processing the request.

    1. Since these are a child/parent relationship, you can create the parent entity and then attached the childern in the foreach loop before you call save changes on the parent entity. EF will automatically populate the foreign key value on the child object with the primary (or associated key) from the parent.
    2. You can continue to use your Entity without having to save it back out to a variable. EF’s object tracking will continue to track this throughout your function.
    3. By moving the savechanges outside of the foreach loop, you are reducing the number of calls. I believe the same amount of SQL will be sent on the one final call, but you may see increases of not having the connection open/close. There may be other built in efficiencies as well from EF

    The Code

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDITED with solution (below...) I have a Splash screen that is packaged into it's
EDIT: solved, look below for my solution. first of all, this is my very
I know that technically all three ways below are valid, but is there any
Inside vb.net the code below works cmd.CommandText = CREATE TABLE Notes ( num INTEGER,
Solution: The problem below was caused by a Divx javascript that overwrote a core
I have a T-SQL that works below: SELECT WP_VTDID AS UTIL_VTDID, (SELECT COUNT(WP_ENGINE) FROM
Hi All below is the code which i use. <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
Below all my code, * Model * Below is Model code, public class MyViewModel
I'm being a little lazy here but are these (below) all the default Backbone
I'm getting the following error: Encoding::UndefinedConversionError: \x81 from ASCII-8BIT to UTF-8 The below all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.