igorr
igorr

Reputation: 141

Entity Framework table constraints

I want to store such object inside the database

public class Account
{
    [Key]
    public int Id { get; set; }

    [Index(IsUnique = true), Required]
    public string UserName { get; set; }

    [Required]
    public string Password { get; set; }

    [Required]
    public string Email { get; set; }
}

Is any way to say to database that property Email must have appropriate form? So that if i want to add account

var account = new Account
{
     UserName = "X",
     Password = "X",
     Email = "NotValidEmail"
}

database will not accept that object.

Upvotes: 2

Views: 83

Answers (2)

Berkay Yaylacı
Berkay Yaylacı

Reputation: 4513

Add this over Email property,

 [EmailAddress(ErrorMessage = "Invalid Mail Address")]

hope helps,

Upvotes: 1

Ioana Stoian
Ioana Stoian

Reputation: 156

Well, personally I don't think that data base should know if the format is right or not. It is your job to be sure it is before adding the object to data base, more details could be found this answer.

Upvotes: 0

Related Questions