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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:10:40+00:00 2026-06-11T15:10:40+00:00

I am using EF code first. Recently I had to replace the following code:

  • 0

I am using EF code first. Recently I had to replace the following code:

User user = userRepository.GetByEmail("some@email.com");

if (user == null)
{
    user = New User { Email = email, CreatedAt = DateTime.Now };

    userRepository.Add(user);
    unitOfWork.Commit();
}

with

Context.ExecuteSqlCommand("IF NOT EXISTS(SELECT 1 FROM Users WHERE Email = '{0}')
                           INSERT INTO Users(Email, CreatedAt)
                           VALUES ('email', GETDATE())");

The reason behind this is that it took EF a very long time to run the first piece of code when trying to add thousands of rows. By changing it to a ExecuteSqlCommand, the time to handle that many rows decreased by a multitude.

The problem I am seeing now (only occurred twice so far) is the following message from the database: Transaction (Process ID 52) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

How would I go about resolving this? Most of my data access is done through EF with a few exceptions like the the one above. I have never seen a deadlock in my logs before so I assume this ha something to do with the query.

My questions are:

  1. Is there a way to write the query using No LOCK? How would that
    query look?
  2. Is there a way to tell EF to use NO LOCK for certain queries?
  • 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-11T15:10:41+00:00Added an answer on June 11, 2026 at 3:10 pm

    What you actually want is to lock the table earlier, not to prevent locking. Locking is necessary to ensure that between the two statements in your command, some other process hasn’t come along and inserted the same user. (Locking is always necessary when inserting data because the physical storage is being modified.)

    Assuming that this is actually the command that is causing deadlock, the following should resolve it, because it only asks for an exclusive lock :

    Context.ExecuteSqlCommand(“IF NOT EXISTS(SELECT 1 FROM Users WHERE Email = ‘{0}’)
    INSERT INTO Users(Email, CreatedAt)
    VALUES (’email’, GETDATE())”);

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

Sidebar

Related Questions

I'm using the Code First approach and have the following Model: public class Person
I'm using Entity Framework Migrations & Code First and recently encountered a very weird
I was working on some code recently and came across a method that had
I'm using Code First with EF 5. Currently I have SPs that return just
Easier to show by example -- I'm using code-first to construct a database. I
When defining a string in a class using Code First, the default for the
I'm trying to learn EF using code first, i dont know how to design
I am trying to teach my self MVC3 and EF4 using code first and
I successfully created database on my local server using Code First, migrated it to
I am using EF Code First to create a database on local .\SQLEXPRESS. Among

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.