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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:46:11+00:00 2026-05-15T09:46:11+00:00

I have a rather deep hierarchy of objects that I’m trying to persist with

  • 0

I have a rather deep hierarchy of objects that I’m trying to persist with Entity Framework 4, POCO, PI (Persistence Ignorance) and Code First. Suddenly things started working pretty well when it dawned on me to not use the new() operator. As originally written, the objects frequently use new() to create child objects.

Instead I’m using my take on the Repository Pattern to create all child objects as needed. For example, given:

class Adam
{
  List<Child> children;
  void AddChildGivenInput(string input) { children.Add(new Child(...)); }
}

class Child
{
  List<GrandChild> grandchildren;
  void AddGrandChildGivenInput(string input) { grandchildren.Add(new GrandChild(...)); }
}

class GrandChild
{
}

(“GivenInput” implies some processing not shown here)

I define an AdamRepository like:

class AdamRepository
{
    Adam Add() 
    { 
        return objectContext.Create<Adam>(); 
    }
    Child AddChildGivenInput(Adam adam, string input) 
    { 
        return adam.children.Add(new Child(...)); 
    }
    GrandChild AddGrandchildGivenInput(Child child, string input) 
    { 
        return child.grandchildren.Add(new GrandChild(...)); 
    }
}

Now, this works well enough. However, I’m no longer “ignorant” of my persistence mechanism as I have abandoned the new() operator.

Additionally, I’m at risk of an anemic domain model since so much logic ends up in the repository rather than in the domain objects.

After much adieu, a question:

Or rather several questions…

  • Is this pattern required to work with EF 4 Code First?
  • Is there a way to retain use of new() and still work with EF 4 / POCO / Code First?
  • Is there another pattern that would leave logic in the domain object and still work with EF 4 / POCO / Code First?
  • Will this restriction be lifted in later versions of Code First support?

Sometimes trying to go the POCO /
Persistence Ignorance route feels like
swimming upstream, other times it feels
like swimming up Niagra Falls. Still, I want to believe…

  • 1 1 Answer
  • 2 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-15T09:46:12+00:00Added an answer on May 15, 2026 at 9:46 am

    Here are a couple of points that might help answer your question:

    In your classes you have a field for the children collection and a method to add to the children. EF in general (not just Code First) currently requires that collections are surface as properties, so this pattern is not currently supported. More flexibility in how we interact with classes is a common ask for EF and our team is looking at how we can support this at the moment

    You mentioned that you need to explicitly register entities with the context, this isn’t necessarily the case. In the following example if GetAdam() returned a Adam object that is attached to the underlying context then the new child Cain would be automatically discovered by EF when you save and inserted into the database.

    var adam = myAdamRepository.GetAdam();

    var cain = new Child();

    adam.Children.Add(cain);

    ~Rowan

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

Sidebar

Related Questions

I have rather large project that uses ICU regex classes. Basically it might run
I have some rather odd behavior in my D program that I've narrowed down
I have a rather enormous project in which I'm trying to retrofit in-memory data.
I have a rather huge query that is needed in several stored procedures, and
I got a rather big class library that contains a lot of code. I
Explanation: Let's say I have an object graph that's nested several levels deep and
I have rather class using TcpClient that spins of a Thread doing while (!streamReader.EndOfStream)
I have a table that stores the hierarchy of workgroups in our organization. It
Have a rather simple question. Does anyone knows if i can use jparallax both
I have a rather large text corpus, of which I would like to check

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.