Since I have started using DATABASE FIRST approach of entity framework. It's easy to catch on and interesting but there's one thing that puzzles me.
In MVC we usually make view models (models with plus-minus properties so it adheres to the particular view). Fine but while using the EF with DB first approach this seems to be impossible. Because we create one model off a database table and then keeps on using it for insert, update, select or anything.
e.g.
Services model:
namespace ZahidCarWash.Models.EF
{
using System;
using System.Collections.Generic;
public partial class Services
{
public short ServiceID { get; set; }
public string ServiceName { get; set; }
public Nullable<decimal> ServicePrice { get; set; }
public Nullable<System.DateTime> EntryDateTime { get; set; }
public Nullable<bool> IsOwner { get; set; }
public Nullable<decimal> Commission { get; set; }
}
}
and then using it in controllers.
[HttpPost]
public JsonResult UpdateServices(Services UpdateServicesVM)
{
ServicesRepository ServicesRep = new ServicesRepository();
int i = 0;
//i = ServicesRep.UpdateServices(UpdateServicesVM, out ReturnStatus, out ReturnMessage);
ZahidCarWashDBEntities zjdb = new ZahidCarWashDBEntities();
zjdb.Entry(UpdateServicesVM).State = EntityState.Modified;
i= zjdb.SaveChanges();
return Json(new { ReturnMessageJSON = i > 0 ? "Success" : "Error", ReturnStatusJSON = i > 0 ? true : false });
}
Or
[HttpPost]
public JsonResult AddServices(Services AddServicesVM)
{
ServicesRepository ServicesRep = new ServicesRepository();
int i = 0;
//i = ServicesRep.InsertServices(AddServicesVM, out ReturnStatus, out ReturnMessage);
ZahidCarWashDBEntities context = new ZahidCarWashDBEntities();
context.Services.Add(AddServicesVM);
context.SaveChanges();
return Json(new { ReturnMessageJSON = ReturnMessage, ReturnStatusJSON = ReturnStatus });
}
I created other customized models by adding/removing properties but that doesn't sound good. Any decent way to do or EF provides?