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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:48:08+00:00 2026-05-18T08:48:08+00:00

Rails has a very convenient uniqueness validation. ASP.NET MVC doesn’t. I need to make

  • 0

Rails has a very convenient uniqueness validation.

ASP.NET MVC doesn’t.

I need to make sure that the e-mail address a user has entered hasn’t been registered by anyone yet.

I can see only one way of doing this kind of validation: create a new data context object in the UniqueAttribute class.

But I’m afraid that wasting memory on a new data context object just for one validation is dangerous.

Am I wrong? Is there a better way to do that?

Update

This is what I got so far

public class UniqueEmailAttribute : ValidationAttribute {
    public override bool IsValid(object value) {
        DataContext db = new DataContext();
        var userWithTheSameEmail = db.Users.SingleOrDefault(
            u => u.Email == (string)value);
        return userWithTheSameEmail == null;
    }
}

// Usage
[UniqueEmail(ErrorMessage="This e-mail is already registered")]
public string Email { get; set; }

There are two problems.

  1. It would be good to have just one UniqueAttribute class, not separate classes for e-mails, usernames etc. How can I do that?

  2. Creating a new data context every time you need to validate a single attribute.

SOLUTION

So in the end I created a unique constraint on the table and now I just have to intercept SqlException in Users repository. Works great and is probably more efficient than searching for the same node in the whole table. 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-18T08:48:09+00:00Added an answer on May 18, 2026 at 8:48 am

    A foolproof way of doing this is to create a validation attribute that would query the database for the email address. It would certainly add latency.

    An alternative would be to create a unique constraint on the table and intercept SqlException.

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

Sidebar

Related Questions

I've been using rails, merb, django and asp.net mvc applications in the past. What
I have a Rails application that has to co-exist with a very old legacy
Rails has a nice idiom that makes it easy for you to have a
Rails has a method form_for which takes a &proc that contains the content present
I'm aware that Rails has a few different levels for logging, but how can
The Paperclip plugin for Rails has a resize option that keeps the image in
Ruby on Rails has a screencast presentation they use to promote their framework that
My Rails application has a simple yaml file that's downloaded relatively frequently (5x per
I am building a rails app that has a number of models, one of
I have a Ruby on Rails project that has been untouched for a while,

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.