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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:44:16+00:00 2026-06-03T06:44:16+00:00

Since there is not any way to add unique constraint to entities in current

  • 0

Since there is not any way to add unique constraint to entities in current latest EF version I came up with an idea. ( I know constraints can be added by embedding SQL )

I added a method to my Repository class :

private IDbSet<T> Entities;

public virtual bool Contains(Func<T, bool> predicate)
{
        return Entities.Any(t => predicate(t));
}

I think this will replace my current duplicate prevention logic:

bool already exists = (from t in someSet.Entities 
                       where t.UniqueColumn == entity.UniqueColumn select t).
                       FirstOrDefault() != null;

if(alreadyExists)
throw new Exception("Entity already exists in DB.");

Is this a good idea, what is the best way to prevent duplicate insertions / updates to database?

EDIT: I edited the Contains method to use a predicate. Now I can write a code like this:

if(repository.Contains(t=> t.Email == "someemail@email.com")
     throw new Exception("Email already exists.");
  • 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-03T06:44:17+00:00Added an answer on June 3, 2026 at 6:44 am

    If this is going to be a rare situation, then there’s nothing wrong with placing a unique constraint on the database and then catching the exception that gets generated when you try to insert an existing record.

    If this is likely to happen a lot, then I would still place the constraint on the database, but do the check as you are.

    FWIW, you should make your method take an Expression<Func<T, bool>> so that the query does not get converted to an Enumerable.

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

Sidebar

Related Questions

A quick question, for a quick answer (since I'm not finding any): Is there
Is there any way to use a script to add files to an Xcode
I'm guessing it's not a Perl Compatible Regular Expression, since there's a special kind
Since there is no way to join tables using Google App Engine datastore, I
Since there is no way that you can make the flash object transparent, there
Is there any way to prevent o.GetType() from throwing an exception when called on
I'm wondering if there is any way to include the RequestContext in the django
With Javassist, is there any way to inject code into a native method? In
Since there are no C# source codes for Wait, Pulse, PulseAll methods. Does anyone
Since there is no type in ruby, how do Ruby programmers make sure a

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.