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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:54:38+00:00 2026-06-15T17:54:38+00:00

I’m using Code-First DBContext-based EF5 setup. In DbMigrationsConfiguration.Seed I’m trying to fill DB with

  • 0

I’m using Code-First DBContext-based EF5 setup.

In DbMigrationsConfiguration.Seed I’m trying to fill DB with default dummy data. To accomplish this task, I use DbSet.AddOrUpdate method.

The simplest code to illustrate my aim:

j = 0;

var cities = new[]
    {
        "Berlin",
        "Vienna",
        "London",
        "Bristol",
        "Rome",
        "Stockholm",
        "Oslo",
        "Helsinki",
        "Amsterdam",
        "Dublin"
    };
var cityObjects = new City[cities.Length];


foreach (string c in cities)
{
    int id = r.NextDouble() > 0.5 ? 0 : 1;
    var city = new City
        {
            Id = j,
            Name = c,
            Slug = c.ToLowerInvariant(),
            Region = regions[id],
            RegionId = regions[id].Id,
            Reviewed = true
        };
    context.CitySet.AddOrUpdate(cc => cc.Id, city);
    cityObjects[j] = city;
    j++;
}

I’ve tried to use/omit Id field as well as to use Id/Slug property as update selector.

when Update-Database is run, Id field is ignored and the value is generated automatically by SQL Server and DB is filled with duplicates; Slug selector allows duplicates and on subsequent runs produces exceptions (Sequence contains more than one element).

Is AddOrUpdate method intended to work this way? Should I perform upsert by hand?

  • 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-15T17:54:39+00:00Added an answer on June 15, 2026 at 5:54 pm

    First (no answer yet), AddOrUpdate can be called with an array of new objects, so you can just create an array of type City[] and call context.CitySet.AddOrUpdate(cc => cc.Id, cityArray); once.

    (edited)

    Second, AddOrUpdate uses the identifier expression (cc => cc.Id) to find cities with the same Id as the ones in the array. These cities will be updated. The other cities in the array will be inserted, but their Id values will be generated by the database, because Id is an identity column. It can not be set by an insert statement. (Unless you set Identity Insert on). So when using AddOrUpdate for tables with identity columns you should find another way to identify records because the Id values of existing records are unpredictable.

    In you case you used Slug as identifier for AddOrUpdate, which should be unique (as per your comment). It is not clear to me why that does not update existing records with matching Slugs.

    I set up a little test: add or update an entity with an Id (iedntity) and a unique name:

    var n = new Product { ProductID = 999, ProductName = "Prod1", UnitPrice = 1.25 };
    Products.AddOrUpdate(p => p.ProductName, n);
    SaveChanges();
    

    When “Prod1” is not there yet, it is inserted (ignoring Id 999).
    If it is and UnitPrice is different, it is updated.

    Looking at the emitted queries I see that EF is looking for a unique record by name:

    SELECT TOP (2) 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName], 
    [Extent1].[UnitPrice] AS [UnitPrice]
    FROM [dbo].[Products] AS [Extent1]
    WHERE N'Prod1' = [Extent1].[ProductName]
    

    And next (when a match is found and UnitPrice is different)

    update [dbo].[Products]
    set [UnitPrice] = 1.26
    where ([ProductID] = 15)
    

    This shows that EF found one record and now uses the key field to do the update.

    I hope that seeing this example will shed some light on your situation. Maybe you should monitor the sql statements as well and see if anything unexpected happens there.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.