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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:56:35+00:00 2026-05-25T22:56:35+00:00

The problem is I’m trying to find a single method to save an entity,

  • 0

The problem is I’m trying to find a single method to save an entity, be it a new entity or an already loaded entity. Most all code samples separate Add and Save methods which goes against so many other current designs of having a single Save() method.

Let me state I’ve read the other posts. I’ve looked at the team blog on repository pattern as well which has a single AddObject() method that I can’t seem to find by assuming it just is an Add(object).

If we load an object and save it, the context knows about it.
A new object we MUST add.
If we add an existing object – it will send it over as an insert.

I want one save procedure here. A user shouldn’t have to know to call Add or Update, a Save() should suffice. With that said, whats the best route here for a single save solution that covers in the same method (only the save, not the load):
1. loading an entity, saving back to the db.
2. Creating a new entity, saving it to the db.
So I’m looking for an implementation that in all cases can just be

ICustomerRepository.Save(Customer customer)

What solution would you be using that works?

The following code in a Save(entity) method seems to work for me


//If the primary key is empty, its a new record
if (order.OrderId == 0)
{
   _context.Orders.Add(order);
}

if (_context.Entry(order).State == System.Data.EntityState.Detached)
{
   //Need to set modified so any detached entities are updated otherwise they won't be sent across to the db. Since it would've been outside the context, change tracking wouldn't have occurred anyways so we have no idea about its state - save it!
   _context.Orders.Attach(order);
   _context.Entry(order).State = System.Data.EntityState.Modified;
}


The issue here though is with a Generic Repository implementation there is no clear way here without querying what the key is and checking its value then via reflection which isn’t clean.

  • 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-25T22:56:35+00:00Added an answer on May 25, 2026 at 10:56 pm

    Generic repository is fairy tale. The only valid usage of repository pattern is specific repository and specific repository knows what type the key is and what is its default value. In the same way you can complain that your generic code will not work if your keys are not generated in the database but in the application.

    EF doesn’t have any support for single method processing and it cannot be simply added. To make it work correctly you would need something on SQL level to do conditional insert / update (or merge in SQL server 2008). EF doesn’t have anything like that and it separates both operations all the way to command execution.

    That means the only way to achieve that is either using some “knowledge” as you shown in your code sample or by checking existence of the entity in database with separate database call or using stored procedure.

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

Sidebar

Related Questions

Problem: Given a list of strings, find the substring which, if subtracted from the
PROBLEM: Here's the code: import matplotlib.pyplot as plt plt.bar([1,2],[5,4]) plt.title('this is a very long
Problem: Find people whose birthdays are tomorrow (table a), who havent got a record
Problem: I'm attempting to disable a radio button by using the code below. This
Problem, simple and annoying. Im just trying to print a list of names, collected
Problem: Every time I start a new session of Internet Explorer, the web application
Problem I need to replace all asterisk symbols('*') with percent symbol('%'). The asterisk symbols
Problem: Trying to create a Mix that is applied to the AVPlayerItem, but it
PROBLEM: I need to write an advanced search functionality for a website. All the
Problem: I'm trying to load a stylesheet in Java but I get an error

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.