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 5973681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:50:04+00:00 2026-05-22T20:50:04+00:00

working with EF4, model first approach in VS 2010: Consider the following EntityModel: OrderBase

  • 0

working with EF4, model first approach in VS 2010:

Consider the following EntityModel:

“OrderBase” is an abstract entity with just one property “Name”

“Detail” (with one property “Text”) is an entity that has a many to one association to “OrderBase” (i.e. one OrderBase has multiple Details)

“Comment” (with one property “Text”) is an entity that has a many to one association to “OrderBase” (i.e. one OrderBase has multiple Comments)

“MusicOrder” is an entity derived from “OrderBase” with just one property “Quantity” (int)

“SongOrder” is an entity derived from “OrderBase” with just one property “Length” (int). SongOrder has a many to one relation to MusicOrder (i.e. one MusicOrder has multiple SonOrders)

For a quick overview:
Wanted to post the image of the datamdel, but I’m not allowed… So any questions concerning the datamodel.

So now I generate my database, execute the ddl script and try to run the following code:

using (DataContainer container = new DataContainer())
{
    MusicOrder musicOrder = new MusicOrder{Name = "Oldies", Quantity = 1};
    SongOrder songOrder = new SongOrder {Name = "Great balls of fire", Length = 240};
    songOrder.Details.Add(new Detail{Text = "Song from Jerry Lee Lewis"});
    songOrder.Comments.Add(new Comment{Text = "Cool song"});

    container.OrderBaseSet.AddObject(songOrder); //relevant line
    musicOrder.SongOrders.Add(songOrder);

    songOrder.Details.Clear();
    songOrder.Comments.Clear();
    container.OrderBaseSet.AddObject(musicOrder);
    container.SaveChanges();
}

In this case “SaveChanges” leads to an exception telling me something about missing foreign key relations…

But if I move the line

container.OrderBaseSet.AddObject(songOrder);

after the “Clear”-part resulting in the following code:

using (DataContainer container = new DataContainer())
{
    MusicOrder musicOrder = new MusicOrder{Name = "Oldies", Quantity = 1};
    SongOrder songOrder = new SongOrder {Name = "Great balls of fire", Length = 240};
    songOrder.Details.Add(new Detail{Text = "Song from Jerry Lee Lewis"});
    songOrder.Comments.Add(new Comment{Text = "Cool song"});

    musicOrder.SongOrders.Add(songOrder);

    songOrder.Details.Clear();
    songOrder.Comments.Clear();

    container.OrderBaseSet.AddObject(songOrder); //relevant line
    container.OrderBaseSet.AddObject(musicOrder);
    container.SaveChanges();
}

everything works fine.

As you can see I know how to come around the exception and I know it makes no sense to “double add” the SongOrder. But I’m wondering if this is a bug or a feature. Why is the exception thrown in my first example. Would really appreciate a comprehensive explanation. In my opinion, the entity framework should also be able to deal with the first example and not throw an exception. So I would say it’s a bug.

Feel free to comment 😉

Andi

  • 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-22T20:50:05+00:00Added an answer on May 22, 2026 at 8:50 pm

    This happens because calling AddObject doesn’t add only single entity but all related entities which are not attached to the context as well. So when you call AddObject(songOrder) in the first example you also add Detail and Comment. But after that you call Remove on navigation properties to remove both Detial and Comment. Remove on attached entities will only break the relation but it will not delete Detail and Comment from the context (it will only set their principal relation to null) so once you try calling SaveChanges it will blow up because you are trying to save Detail and Comment without associated OrderBase (I guess relations are non nullalbe).

    In the second example Comment and Detail is deleted before you add order to the context so it behaves as expected.

    It is not a bug it is a “feature”.

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

Sidebar

Related Questions

Im using EF4 in VS2010, POCO's and the model-first approach. My entity has the
I working to code first technique with EF4.1, WCF webservice, and Sql azure. To
I've got an existing EF4.1 project which is working just fine. I've added a
I'm working on a wiki project. MVC3 + EF4 Code First. All classes from
I am working on a project with the following conditions: Visual Studio 2010 ASP.NET
I am working on a project in ASP.NET MVC using EF4 Code-First for the
I am working on my first large project that uses EF4 and have been
Application : I am working on one mid-large size application which will be used
I am using EF4. Just a quick question, is this a really good framework?
I'm working with MVC3 and EF4. I would like to create a multilingual FAQ

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.