user2901979
user2901979

Reputation: 239

How do I create a unique field in MVC Model?

This is my model class in C# MVC project. I want to set Membership Id as a Unique field ?

public class Customer
{
        public int CustomerID { get; set; }
        public string MembershipID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
}

Upvotes: 2

Views: 9356

Answers (3)

Allaoua Youcef
Allaoua Youcef

Reputation: 21

Entity Framework does not support UNIQUE for the attributes so we have to create a function for that. I advise you to useFluentValidation, it's easy to take in hand

public class Customer
{
        public int CustomerID { get; set; }
        public string MembershipID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
}

You would define a set of validation rules for this class by inheriting from

    using FluentValidation;

    public class CustomerValidator : AbstractValidator<Customer> {

    public CustomerValidator() {
        RuleFor(customer => customer.EmailAddress).NotNull().Must(UniqueAddmail);
      }

   public bool UniqueAddmail (string EmailAddress)
        {
            return new AnnuaireDbEntities().Wilaya.FirstOrDefault(x => x.Nom == EmailAddress) == null;
        }
    }

Upvotes: 2

Iulian Radulescu
Iulian Radulescu

Reputation: 324

public class Customer
{
    public int CustomerId { get; set; }
    public string MembershipId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
}

public class Company
 {
     public string MembershipId { get; set; }
     public string Name {get; set;}

 }
public class CustomersBankAccount
 {
   public string Name {get; set;}
   public int CustomerId { get; set; }
 }

The convention when you want to build a database with code first convention is to have a "Id" (foreign key) part integrated in your properties. Of course I recommend that all properties are declared virtual so that EF can grasp into your code and enable some features. like an efficient tracking mechnaism

So : If you have a property on the object named ID, EF assumes the property holds the primary key value and sets up an auto-incrementing (identity) key column in SQL Server to hold the property value.

Edit:

"On the object" meaning object of Customer. Essentially the primary key in your example is in customer , and the foreign key is in all other tables that are related to customer. A primary key cannot be duplicated or null. A foreign key - yes. It is possible that the same customer can have multiple bank accounts. :) So if you are wondering , where is the plumbing to set your keys , stop. MVC can just look in the code above and decide (on a scaffolding selection) how to make the tabels in your DB. You don't have to indicate nothing in your code apart from what you allready seen (the "Id" an the end )

Edit based on my comment:

  public class Customer
{
    public int CustomerId { get; set; }
    [Remote("CheckMembershipIdActionMethod", "CorrespondingController")]
    public string MembershipId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
}

Upvotes: 0

Marco
Marco

Reputation: 23945

I can only guess, since you did not mention what the key is in your class.

Entity Framework does not support unique columns as per DataAnnotations. The only thing you can do is set one column as the primary key in combination with the other ID column being a Guid.

Assuming CustomerID is your primary key:

using System.ComponentModel.DataAnnotations;

public class Customer
{
    [Key]
    public int CustomerID { get; set; }
    public Guid MembershipID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
}

assuming your MembershipID is your primary key:

using System.ComponentModel.DataAnnotations;

public class Customer
{

    public Guid CustomerID { get; set; }
    [Key]
    public int MembershipID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
}

By making one of the columns a guid your chances of generating 2 keys of the same value are near zero.

Upvotes: 0

Related Questions