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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:46:22+00:00 2026-06-08T03:46:22+00:00

I would like to add a new entity to a database having used EF

  • 0

I would like to add a new entity to a database having used EF to retrieve other instances of the data. I was hoping to use code such as below:

using (var db = new FundResearchContext(null))
{
    var query = from a in db.As select new 
                {
                    a = a,
                    bs = (from b in db.Bs where b.AnId == a.AnotherId select b)
                };

    var listOfAsAndRelatedBs = query.ToList();
    listOfAsAndRelatedBs[0].bs.Add(new B());
    db.SaveChanges();
}

Whilst the objects in the bs collection are tracked and changes to them are saved, I can’t add new items to the collection because it is of type IQueryable. I realise that in a simple case like this I could put properties on A an B to indicate they are linked via a Foreign Key, but that approach is not posssible in my real life scenario. So is there an alternative syntax that would make bs itself a tracked object to which I could add new B objects and have them persisted? Or do I have to do it all manually using db.Bs.Add(new B())?

  • 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-08T03:46:24+00:00Added an answer on June 8, 2026 at 3:46 am

    If your A class has a Bs collection on it, you can say:

    var query = from a in db.As select new 
                {
                    a = a,
                    bs = a.Bs
                };
    

    This has a nice eager-loading effect, since all of the Bs for each A are loaded in and attached when this query is materialized. And you should be able to add objects to either the bs property or the a.Bs property of one of the results.

    If you don’t have this relationship set up between your Entity classes, you’re not going to be able to do this, and you’ll be stuck adding to db.Bs directly. Of course, you can create a sort of shortcut object to facilitate this:

    public class DTO {public A a{get;set;} public IObjectSet<B> allBs {get;set;} }
    
    var list = (from a in db.As 
                select new DTO
                {
                    a = a,
                }).ToList();
    foreach(var a in list)
    {
        a.allBs = db.Bs;
    }
    

    That way you don’t have to have access to the context directly to add an item to the Bs.

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

Sidebar

Related Questions

PHP/MySQL (CodeIgniter) I would like to add new interest_keywords in the exist database value.
I have a dataframe and would like to add a new column where the
Using the .NET System.ServiceModel.Syndication classes... I would like to add a new SyndicationElementExtension to
I would like to add some paragraphs or new lines or words dynamically but
I would like restfully add a parameter to the new named path. So for
I would like to be able to add a new SQL LOGIN and name
I'm new in web development and would like to add a feature to a
I would like to make use @IndexColumn to set seq number of some data
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add

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.