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

  • Home
  • SEARCH
  • 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 3609168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:35:34+00:00 2026-05-18T21:35:34+00:00

I will compare 2 scenarios to add Rsvp rows. Which one do you prefer

  • 0

alt text

I will compare 2 scenarios to add Rsvp rows. Which one do you prefer in production?


METHOD 1: Adding a new Rsvp object to datacontext’s Rsvp collection

Rsvp r = new Rsvp();
r.AttendeeName = "xport";

r.DinnerId = 1;//there will be an exception if it is set to a Dinnner object that does not exist.

entities.Rsvps.AddObject(r);

entities.SaveChanges();

We will get an exception if we attempt to set DinnerId to a Dinner object that does not exist. This behavior is consistent and straightforward.


METHOD 2: Adding a new Rsvp object to a Dinner object’s Rsvps properties

Rsvp r = new Rsvp();
r.AttendeeName = "xport";

r.DinnerId = 10000;//this Dinner does not exist!

Dinner d = entities.Dinners.First(x => x.DinnerId == 1);

d.Rsvps.Add(r);

entities.SaveChanges(); 

The foreign key property DinnerId of a Rsvp object can be set to any number. When this Rsvp object is added to a Dinner object’s Rsvps collection, the DinnerId will be overriden silently. Example above shows DinnerId is set to 10000 which is an Id of an Dinner object that does not exist. Is it an unavoidable behavior?

  • 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-05-18T21:35:34+00:00Added an answer on May 18, 2026 at 9:35 pm

    Definetely method 2.

    Why? Because a Rsvp cannot exist without a Dinner. In this relationship, Dinner is the parent – and if we were to create a Repository, we would create a DinnerRepository (as Dinner is the “aggregate root” in DDD terms)

    In regards to your note – yes, this is avoidable behaviour – what you should do is don’t expose the foreign keys on the model. This is an option available when you create/update your model.

    This way, the relationships must be created/modified via the entity:

    Rsvp r = new Rsvp();
    r.AttendeeName = "xport";
    r.DinnerId = 10000; // this throws a compiler error. good! we do not want people tinkering with FK's.
    Dinner d = entities.Dinners.First(x => x.DinnerId == 1);
    d.Rsvps.Add(r); // this is the correct way to add a RSVP
    entities.SaveChanges(); 
    

    In other words – the only way to create/modify a Rsvp is via a Dinner – and the FK property cannot be modified.

    Which makes sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a Powershell script which will compare two databases and come up with
Will limiting a query to one result record, improve performance in a large(ish) MySQL
Will .Net 4.0 use a new version of the CLR (v2.1, 3.0) or will
Will everything go to the cloud? If so, in which direction? Google-Gears? Flash,AIR,JavaFX? See
I have to support a new input file format in a system which uses
I know that equals() will compare the value of objects, the '==' operator will
I'm trying to write a function in three classes that will compare two lists
I am trying to create a query that will compare an email with ones
Will the code below work if the clock on the server is ahead of
Will I have to pay again? I have about 9 months left before renewal

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.