2

I'm developing my first MVC5 website and it happens this is also the first time I'm using ET.

I'm using Database First approach.

For example, lets say these are my fields in Users table.

| Username | Email | Password | 

And Entity Frameworks generate me the following class:

class Users
{
    public string Username { get; set; }   
    public string Email { get; set; }
    public string Password { get; set; }
}

Now lets say I want to create a View for Registration. This registration requires user to confirm his password. Do I expand the existing ET generated class like this?

class Users
{
    public string Username { get; set; }   
    public string Email { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
}

Or do I make an entirely different class myself that will contain all the necessary information separately from ET generated class?

Do I make Views using ET generated classes, or do I use my own classes?

I've seen ViewModels being mentioned here and there, but it is not very clear to me as to what purpose they serve.

As of right now, I'm manually adding extra fields to ET classes, and it works, but I've no idea if I'm doing it wrong or right.

6
  • Why do you want to add new filed for confirm password, you can compare value of two textbox on a view. Commented Oct 16, 2016 at 13:39
  • Compare on POST Controller? On something like WebForms I'd compare two TextBoxe values, not sure how it happens on MVC. Commented Oct 16, 2016 at 13:44
  • 1
    You can create a RegisterViewModel. Then to compare those properties, use Compare attribute, exactly like what is used in ASP.NET MVC default project template. Commented Oct 16, 2016 at 13:45
  • @OverflowStack, Yes compare it using client side script. Commented Oct 16, 2016 at 13:46
  • @OverflowStack see here Commented Oct 16, 2016 at 13:49

3 Answers 3

4

You should not touch your entity framework generated code for such requirement. Instead you need to create a view model to contain fields which you want to get from user when registration. You can create a RegisterViewModel. Then to compare those properties, use Compare attribute, exactly like what is used in ASP.NET MVC default project template. Then in controller, check if model state is valid, create a User entity using posted values an save in db:

Model

public class RegisterViewModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, 
    ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", 
    ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

Action

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new User() { UserName = model.UserName, /*... other fields */ };
        // Save user
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. And the Register View would be ''generated'' using RegisterViewModel class as template or Users ET class ?
Yes, in this line var user = new User() { UserName = model.UserName, /*... other fields */ };
The posted code is just based on ASP.NET default project template generated code and you may need to apply some changes based on your requirement. But it's a good sample code to share the idea with you and show you the correct way.
Thanks a lot. One last dumb question. Lets say I have both RegisterViewModel class and Register Control. I want to add a new View. On the Add View window, I use RegisterViewModel as Model Class? And property names must match the ones on ET class?
You're welcome. After creating the model and building the project, you can see RegisterViewModel in list of available model classes.
0

Don't expand EF Entities. Make new classes and when you need to transfer data from DB to your View, copy the properties (you can use AutoMapper).

Comments

0

When it comes to Database first approach, you should never try extending your Model because it can cause all sorts of weird behavior.

One of those behaviors you will encounter when you try updating your model from the database, It will automatically remove what you added Manually.

I would recommend you to go for the ViewModelExample mentioned in answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.