0

So I am coding my UserAccount model which will have an encrypted password. I am using entity framework code first.

My problem now is that I will store an encrypted password in my database, so length in the field password will not be the same than the one stored in the database.

But if I set string length attribute to 20 [StringLength(20)], it will be 20 for database, 20 for password field.

I can’t find the way to specify the correct data annotation, or is there a better solution for my problem?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
bto.rdz
  • 6,636
  • 4
  • 35
  • 52
  • well i found my answer. Hope it helps someone [StringLength vs MaxLength](http://stackoverflow.com/questions/5717033/stringlength-vs-maxlength-attributes-asp-net-mvc3-with-ef-4-1-code-first) – bto.rdz Dec 05 '13 at 00:17

2 Answers2

0

I would use a view model in this situation.

Have your model actually represent what is in the database and use your view model to add ui validation as well as limit what the view actually has access to.

  • 1
    Not enough reputation to comment on mikhairu's post, but I typically create a ViewModel folder (mikhairu showed this in his example). You also may want to look into AutoMapper to handle getting data from one model to the other and vice versa. – Doug Marzella Dec 05 '13 at 01:02
0

A better solution would be to have ViewModels for your Views and Models for your code-first approach. This way your presentation is separate from your domain model.

I'll give you an example. Let's say your custom login page has Username and Password text fields, whereas your corresponding code-first model might have the following properties in it:

-Id
-Username
-Password
-DateOfRegistration
-IsApproved

So you wouldn't want to show all of these in a View. That's why you create a ViewModel that contains only properties that pertain to a View (i.e. Username, Password).

The domain models (code-first models) go in a Models/ directory, where as ViewModels normally go to ViewModels/ directory.

Your project directory structure might be as follows.

App_Start/
Areas/
Content/
Controllers/
    LoginController.cs
Models/
    Users.cs
    ...
Scripts/
Views/
    Login/
    ...
ViewModels/
    Login/
        LoginViewModel.cs
...

Your Views would be strongly-typed to a ViewModel.

You can apply attributes specific to a view in a ViewModel. For example, your ViewModel might looks as follows. (Note that ViewModel has view-specific attributes such as Required, Display, etc.)

public class LoginViewModel
{
        [Required]
        [StringLength(30, MinimumLength = 6)]
        [Display(Name = "Username")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100)]
        [DataType(DataType.Password)]
        public string Password { get; set; }
}

And your code-first model might look as follows.

public class Users
{
    public int UserId { get; set; }

    [StringLength(30)]
    public string UserName { get; set; }

    [StringLength(20)]
    public string Password { get; set; }

    public DateTime? DateOfRegistration { get; set; }

    public bool IsApproved { get; set; }
}
sakura-bloom
  • 4,524
  • 7
  • 46
  • 61