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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:27:18+00:00 2026-06-14T03:27:18+00:00

In my code I have several lines of code which looks like this: Insertions[sale]

  • 0

In my code I have several lines of code which looks like this:

Insertions["sale"] = (modelEntities, entity) => modelEntities.AddToSale((Sale) entity);

It works fine when there is no such entity in database. But I want to update entity when it is already in database. It can be done via Find method and fields assignment but I want to keep a pretty code and use something like initialization from constructor or Insert-or-Update.

How can I do this?

  • 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-14T03:27:19+00:00Added an answer on June 14, 2026 at 3:27 am

    Personally I think it is useless to build a dictionary with add/insert method delegates. It’s a lot of repetitive code. The context has generic methods to insert or update any entity object. You can even make one method that does both, for example (based on ObjectContext):

    public static void Upsert<T>(this ObjectContext context, T entity, int key)
      where T : EntityObject
    {
      if (entity != null)
      {
        ObjectSet<T> objectSet = context.CreateObjectSet<T>();
        if (key > 0)
        {
          if (entity.EntityState == EntityState.Detached)
          {
            objectSet.Attach(entity);
            context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
          }
        }
        else
        {
          objectSet.AddObject(entity);
        }
      }
    }
    

    If you want you can even factor out the key argument by getting the key field from the EF model and doing some reflection on the entity, but that probably slows down the process remarkably.

    Note that an Attached persisted object is ignored by this method: it is assumed that it will be saved by the context it already belongs to.

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

Sidebar

Related Questions

I have seen example code that looks like this, which seems perfectly reasonable: [Authorize(Roles
I have several HTML code combinations like this: <h2></h2> <p></p> <iframe></iframe> <hr/> and from
We have several areas where code like this can be seen public Map extractData(ResultSet
I have managed to crunch down a several line code to this For Each
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I have several columns that I'd like to search against. My code might not
I have code which issues many HTTP GET requests using Python's urllib2, in several
Context I have 3 buttons on several lines (edit, delete, unsuscribe). This 3 buttons
Issue: I have a markup like this (only the important lines): <%@ Control Language=C#
I've worked out most of the code and have several game classes. The one

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.