I have 2 layers in my web app. 1. is business layer with domain models and context classes for each model. 2. is user layer with view models, and it should be with controllers and views.
My task is to make CRUD over domain models but also over view models. So I've got 2 context classes (one in first layer and second in second layer) that is error.
How can I make CRUD over both models (domain and viewmodels) without having two context classes?
When I make a controller with contextClass from domain model I've got an error --> show you below in controller 'details, edit and delete actionResult'
E.g.
Public class Car:ICar
{
[Key]
public int I'd{get; set;}
public string Name{get; set}
}
//viewModel
Public class ViewCar:Car
{
[Display(Name="Cars model")]
public string Name{get; set;}
}
//controller
public ActionResult Details(int? I'd)
{
if(id==null)
{return new HttpStatusCodeResult(HttpStatusCode.BadRequest);}
ViewCar viewCar=db.Cars.Find(id);
if(viewCar==null)
{return HttpNotFound();}
return View (viewCar);
}
Error is in line where I need to find by id and it's says
"Cannot implicitly convert type 'Project.Services.Car' to 'Project.MVC.ViewModels.ViewCar'. An explicit conversion exists (are you missing a cast?)"
My another question is how can I cast one project to another?
What I have tried:
I have read all over internet and there's are similar answers from int to string and so on but I didn't find any examples from casting one project to another. So please if someone can give me an explanation on how I can do that cast? And how to make CRUD over models in business layer and over viewModels in users layer and with one contextClass (how it should be in the first place)?