Reputation: 7164
I want to make Email field unique. It's a field of ApplicationUser (of IdentityUser) That's what I did : namespace BidProject.Models {
public class ApplicationUser : IdentityUser
{
public int UserID { get; set; }
[Index(IsUnique = true)]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I have these references:
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
I get these errors for that :
The type or namespace name 'Index' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'IndexAttribute' could not be found (are you missing a using directive or an assembly reference?)
How can I make that field unique? Or can you tell me how IdentityUser's UserName field is unique? Thanks.
Upvotes: 0
Views: 1185
Reputation: 1855
Add this:
using System.ComponentModel.DataAnnotations.Schema;
If that doesn't work, check if you have a reference to EntityFramework.dll.
Upvotes: 1
Reputation: 21500
Looks like you are missing a namespace at the top of the file, or a project reference to EntityFramework.dll? Try putting your cursor inside the word 'Index', then hit ctrl-. -- you should see a popup menu offering something like 'resolve reference' or 'add using for using System.ComponentModel.DataAnnotations.Schema;'
If you've got, say, a .net 4.0 project referencing a .net 4.5 assembly, sometimes that'll break things. You'll need to upgrade the project to a more recent version of the framework.
Upvotes: 0
Reputation: 4230
You should specify a name for the Index. Use the below syntax.
[Index("IX_EmailUnique", 1, IsUnique = true)]
Upvotes: 0