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

  • Home
  • SEARCH
  • 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 9246241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:26:56+00:00 2026-06-18T09:26:56+00:00

first, I was only trying to update a modified model. Lets say, we talk

  • 0

first, I was only trying to update a modified model. Lets say, we talk about “Article” as a model.
The following method is implemented in a class called “Articles”:

    public static void SaveArticle(Article article) 
    {
        if (article.Id == 0)
        {
            webEntities.Articles.Add(article);
        }
        else
        {
            webEntities.Articles.Attach(article);
            webEntities.Entry(article).State = EntityState.Modified;
        }

        webEntities.SaveChanges();
    }

So whenever I want to save an modified article in a controller action, I just have to call “Articles.SaveArticle(myArticle);”, which works as expected.
So far so good but this means I would need to implement this redundantly for every model/entity.

Then I thought about something like a template-pattern. I.e. a class called “Entity” where “Article” inherits from “Entity”.
Furthermore, a class called “Entities” contains a static method like this:

    public static void SaveEntity(Entity entity) 
    {
        if (Entity.Id == 0) // <-- Problem 1
        {
            webEntities.Entities.Add(entity); // <-- Problem 2
        }
        else
        {
            webEntities.Entities.Attach(entity); // <-- Problem 3
            webEntities.Entry(entity).State = EntityState.Modified; // <-- Problem 4
        }

        webEntities.SaveChanges();
    }

So I would not need to implement it redundantly but I don’t know how to solve the problems mentioned in the code above.
Do I think too complicated or what would be a best practice to my problem?

Thanks in advance!

Kind regards

  • 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-18T09:26:57+00:00Added an answer on June 18, 2026 at 9:26 am

    Use generics.

    public static void Save<T>(T entity)
        where T : class
    {
        webEntities.Set<T>().AddOrUpdate(entity);
        webEntities.SaveChanges();
    }
    

    AddOrUpdate is an extension method in System.Data.Entity.Migrations.

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

Sidebar

Related Questions

this code throw exception while i am trying to update value ,first value only
Hi When i'm trying to update a jdbc table only first row gets updated
I'm trying to import an excel file Excel97-2003 (the first sheet only) into an
I am trying to iterate only the first n values in my Map, is
I am trying to stitch two images together, but only the first one can
I'm trying to figure out why my NSTextFields are only retained in the first
I have the following script which first shows only the first para of the
I'm trying to update a table to remove all but the first instance of
I am trying to only fetch the first record in my table for display.
I am trying to execute two sql statements in a row. EDIT:Only the first

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.