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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:39:34+00:00 2026-05-25T23:39:34+00:00

I have a streets table, which has a combo of two string columns acting

  • 0

I have a streets table, which has a combo of two string columns acting as the PK, being postalcode and streetcode.

With EF4.1 and DBContext, I’d like to write a single “Save” method than takes a street (coming in in an unattached state), checks if it already exists in the database. If it does, it issues an UPDATE, and if it doesn’t, it issues an INSERT.

FYI, the application that saves these streets, is reading them from a textfile and saves them (there a few tens of thousands of these “streetlines” in that file).

What I’ve come up with for now is:

    public void Save(Street street)
    {
        var existingStreet = (
                                from s in streetContext.Streets
                                where s.PostalCode.Equals(street.PostalCode)
                                && s.StreetCode.Equals(street.StreetCode)
                                select s
                             ).FirstOrDefault();

        if (existingStreet != null)
            this.streetContext.Entry(street).State = System.Data.EntityState.Modified;
        else
            this.streetContext.Entry(street).State = System.Data.EntityState.Added;

        this.streetContext.SaveChanges();
    }

Is this good practice ? How about performance here ? Cause for every street it first does a roundtrip to the db to see if it exists.

Wouldn’t it be better performance-wise to try to insert the street (state = added), and catch any PK violations ? In the catch block, I can then change the state to modified and call SaveChanges() again. Or would that not be a good practice ?

Any suggestions ?

Thanks

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

    Thanks for the replies but none were really satisfying, so I did some more research and rewrote the method like this, which satisfies my need.

    ps: renamed the method to Import() because I find that a more appropriate name for a method that is used for (bulk) importing entities from an outside source (like a textfile in my case)

    ps2: I know it’s not really best practice to catch an exception and let it die silently by not doing anything with it, but I don’t have the need to do anything with it in my particular case. It just serves as a method to find out that the row already exists in the database.

    public void Import(Street street)
    {
            try
            {
                this.streetContext.Entry(street).State = System.Data.EntityState.Added;
    
                this.streetContext.SaveChanges();
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException dbUex)
            {
                this.streetContext.Entry(street).State = System.Data.EntityState.Modified;
    
                this.streetContext.SaveChanges();
            }
            finally
            {
                ((IObjectContextAdapter)this.streetContext).ObjectContext.Detach(street);
            }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an table in my database which so far has been great for
I have an old table which has a column like this 1 | McDonalds
I have table named as contacts which has nearly 1.2 million records we use
I have a MySQL table that has about 30k records, of which 2k I
in table I have a several columns which are related to address ( street,
I have a database table structure as follow: datatype data mytable now datatype has
Problem: I have an address field from an Access database which has been converted
I have got a problem with two tables: CREATE TABLE IF NOT EXISTS `addresses`
I have a table in mdb with field address, which will contain street address
I have a database scheme with versioning data rows, e.g. Table Person has the

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.