2

I'm using Database first approach and I want to use my own validation I tried to modify generated model, and it works:

But, if I need to update model from DB, of course my validation code overwrites Entity Data Model and attributes disappear. How can I overcome the situation with my own validation?

So what I end up doing that I have created a within same namespace and I have named ClientViewModel and in this class I have all my custom validation and its works until when I try to save and I get the following error:

The best overloaded method match for 'System.Data.Entity.DbSet.Add(myapp.Models.Client)' has some invalid arguments

here is my controller looks like:

[HttpPost]
   [ValidateAntiForgeryToken]
   public ActionResult Create(ClientViewModel client)
   {
      if (ModelState.IsValid)
      {
        try
         {
            using (var context = new db_entities())
            {
               //more code...        
               db.Clients.Add(client); //<<<ERROR
               db.SaveChagnes();
             }
          }
        }
    }
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 1
    you need use automapper. view this link: http://stackoverflow.com/questions/20635534/simple-automapper-example – Tbseven Jan 30 '15 at 21:51
  • You need to map you view model to the data model and save the data model –  Jan 30 '15 at 21:51
  • i have the same prop what `Client` and `ClientViewModel` has – Nick Kahn Jan 30 '15 at 21:52
  • you may know that, but the compiler doesn't. – Claies Jan 30 '15 at 21:53
  • Make no difference, they are different types –  Jan 30 '15 at 21:53
  • @StephenMuecke: can you please elaborate little bit more on types, I even have the same namespace. an example would be better – Nick Kahn Jan 30 '15 at 21:56
  • @AbuHamzah, You have a data model which is typeof `Client` (i.e `public class Client { ..}`, but your view model is type of `ClientViewModel` (i.e. `public class ClientViewModel { ..}`. You db context expects typeof `Client` but you are passing it an instance of `typeof `ClientViewModel` which causes the error –  Jan 30 '15 at 22:00
  • what is the best way to deal just map the view model?, please reply as answer so i can accept that and it would be good if you provide with an example – Nick Kahn Jan 30 '15 at 22:03
  • There are a number of options, including getting the original data model then updating its properties from the view model, creating a new data model e.g. `var c = client.Select(x => new Client() { ID = x.ID, Name = x,Name ...});` and adding it to the context, or using tools such as [automapper](https://github.com/AutoMapper/AutoMapper) –  Jan 30 '15 at 22:14

3 Answers3

1

You will need to convert the viewmodel into a model class that EF tracks. Assuming both your ViewModel and Model have 3 properties x, y and z, you can see something like this:

public Client ConvertViewModelToModel(ClientViewModel vm)
    {
        return new Client ()
        {
            x = vm.x,
            y = vm.y,
            z = vm.z
        };
    }

And then call this to convert the viewmodel:

var clientModel = ConvertViewModelToModel(vm);

You can then go about adding this clientModel to the db.Clients collection and go about saving it.

Yasir
  • 1,595
  • 5
  • 23
  • 42
1

I end-up using AutoMapper and its a tedious situation without using the AutoMapper It not only reduces the effort, but also limits the execution time that has been taken by such a large number of lines to execute.

Here is my updated code:

  Client clientModel = Mapper.Map<ClientViewModel, Client>(client);
  db.Clients.Add(clientModel);
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
0

Check the database to be created properly. if not then try :

>Enable-migrations
>add-migration model
>update-database

Maybe solved by this. I tried it which solved mine.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Urvish
  • 1