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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:19:19+00:00 2026-06-08T13:19:19+00:00

I have the following code (Database is SQL Server Compact 4.0): Dim competitor=context.Competitors.Find(id) When

  • 0

I have the following code (Database is SQL Server Compact 4.0):

Dim competitor=context.Competitors.Find(id)

When I profile this the Find method takes 300+ms to retrieve the competitor from a table of just 60 records.

When I change the code to:

Dim competitor=context.Competitors.SingleOrDefault(function(c) c.ID=id)

Then the competitor is found in just 3 ms.

The Competitor class:

Public Class Competitor
    Implements IEquatable(Of Competitor)

    Public Sub New()
        CompetitionSubscriptions = New List(Of CompetitionSubscription)
        OpponentMeetings = New List(Of Meeting)
        GUID = GUID.NewGuid
    End Sub

    Public Sub New(name As String)
        Me.New()
        Me.Name = name
    End Sub

    'ID'
    Public Property ID As Long
    Public Property GUID As Guid

    'NATIVE PROPERTIES'
    Public Property Name As String

    'NAVIGATION PROPERTIES'
    Public Overridable Property CompetitionSubscriptions As ICollection(Of CompetitionSubscription)
    Public Overridable Property OpponentMeetings As ICollection(Of Meeting)
End Class

I defined the many to many relations for CompetitionSubscriptions and OpponentMeetings using the fluent API.

The ID property of the Competitor class is a Long which is translated by Code First to an Identity column with a primary key in the datatable (SQL Server Compact 4.0)

What is going on here??

  • 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-08T13:19:21+00:00Added an answer on June 8, 2026 at 1:19 pm

    Find calls DetectChanges internally, SingleOrDefault (or generally any query) doesn’t. DetectChanges is an expensive operation, so that’s the reason why Find is slower (but it might become faster if the entity is already loaded into the context because Find would not run a query but just return the loaded entity).

    If you want to use Find for a lot of entities – in a loop for example – you can disable automatic change detection like so (can’t write it in VB, so a C# example):

    try
    {
        context.Configuration.AutoDetectChangesEnabled = false;
        foreach (var id in someIdCollection)
        {
            var competitor = context.Competitors.Find(id);
            // ...
        }
    }
    finally
    {
        context.Configuration.AutoDetectChangesEnabled = true;
    }
    

    Now, Find won’t call DetectChanges with every call and it should be as fast as SingleOrDefault (and faster if the entity is already attached to the context).

    Automatic change detection is a complex and somewhat mysterious subject. A great detailed discussion can be found in this four-part series:

    (Link to part 1, the links to parts 2, 3 and 4 are at the beginning of that article)

    http://blog.oneunicorn.com/2012/03/10/secrets-of-detectchanges-part-1-what-does-detectchanges-do/

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

Sidebar

Related Questions

I have the following code to connect to a sql server compact edition 2008:
I have the following SQL code that runs against a Change Request database. Each
I have the following setup: Sharepoint 2010 with SQL Server and Office 2010. This
I have SQL Server Compact 3.5 database that contains account information. But everytime I
I have an SQL Server database table created by deploying the following description in
I have the following from a Microsoft SQL Server database for date/time value: 0x00009CEF00A25634
I have following code for inserting data into database using PDO. It inserts data
I have the following code: //-------------------------------------------------------------------------- // 2) Query database for data //-------------------------------------------------------------------------- $result
I have the following code which does a bit of housekeeping on a database:
I have the following code where I make an copy of my database that

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.