2

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.

igorr
  • 141
  • 8

2 Answers2

1

Add this over Email property,

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

hope helps,

Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
0

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.

Community
  • 1
  • 1
Ioana Stoian
  • 146
  • 1
  • 10
  • Assume that application has two layers. 1. DataStorage layer, 2.Presentation layer. The second layer could be implemented by many people, but storage layer would be constant. At that point i want to have mechanism to prevent adding wrong data that comes from presentation layer, becouse that data probably is not mine. – igorr Aug 24 '16 at 11:04