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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:44:10+00:00 2026-05-24T23:44:10+00:00

How can I create+persist+have-a-proxy-to a new instance of a code-first poco using only navigation

  • 0

How can I create+persist+have-a-proxy-to a new instance of a code-first poco using only navigation property collections? In the bellow code, I show how you might want to do this, if using member functions to create POCOs that then create POCOs. You don’t have a DbContext, but if you create an object and persist it using DbSet.Add, the returned object isn’t a proxy, so you can’t in turn use its DbSet.Add to add a different sub-object.

In this code, if you call MailingList.AddIncomingMessage(“my message”), you get an exception at the “OOPS” comment, because the created msg isn’t a proxy and thus its Message.doodads property is null.

class Doodad {
  public int ID { get; set; }
  public string doodad { get; set; };
}

class Message {
  public int ID { get; set; }
  public virtual MailingList mailingList { get; set; } 
  public virtual ICollection<Doodad> doodads { get; set; }
  public string text { get; set; }

  public void GetDoodadCreateIfNeeded(string doodad) {
    try {
      // won't be found since we just created this Message
      return this.doodads.First(d => d.doodad == doodad);
    } catch (Exception e) {
      Doodad newDoodad = new Doodad() { doodad=doodad };

      // OOPS! this.doodads == null, because its not a proxy object
      this.doodads.Add(newDoodad);
      return newDoodad;
    }
  }
}

class MailingList {
  public int ID { get; set; }
  public virtual ICollection<Message> messages { get; set; }

  public void AddIncomingMessage(string message) {
    var msg = new Message() { text=message };

    // we have no Context, because we're in a POCO's member function
    this.messages.Add(msg);

    var doodad = msg.GetDoodadCreateIfNeeded("bongo drums");
  }
}

EDIT: sorry guys, I forgot to put the property accessors and ID in for this simplified case, but I am using them in the actual code.

  • 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-24T23:44:11+00:00Added an answer on May 24, 2026 at 11:44 pm

    It has nothing to do with proxies. It is the same as any other code – if you want to use object / collection you must first initialize it! Your fist command:

    return this.doodads.First(d => d.doodad == doodad);
    

    doesn’t throw exception because it didn’t find doodad but because the doodads is null.

    What do you need to do? You need to initialize collections before you first use them. You can do it:

    • Directly in their definition
    • In entity’s constructor
    • In property getter (lazy initialization) once they are first needed – that would require to change your fields to properties which is btw. correct way to write classes in .NET
    • In your custom methods you can check if they are null and initialize them
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
How can create a New user in ORACLE with full access (alter, delete, select,
I can create a sparse php array (or map) using the command: $myarray =
I can create new Silverlight App with .NET RIA Services enabled. But when I
This question regards how one can effectively create and persist event domain objects on
Have a boolean field (using formtastic) and the value won't persist through an error.
I can create a menu item in the Windows Explorer context menu by adding
I can create a literal long by appending an L to the value; why
I can create a contact that is not mail enabled, but how do I
How can create a view with mutiple images, something similiar to the photo app

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.