I created a view using Microsoft SQL Server that has joins to pull up all the needed information from my 4 different tables. In Visual Studio I am using ASP.NET to create a webpage to access my database. I have pulled up my view which looks great, however when I go to add a new entry it tells me:
'InnerException {"View or function 'dbo.VehiclesView' is not updatable because the modification affects multiple base tables."}'
How can I fix this issue?
Here is my VehiclesView Code:
<pre lang="c#">public class VehiclesViewsController : Controller
{
private DataConnection db = new DataConnection();
public ActionResult Index()
{
return View(db.VehiclesViews.ToList());
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
VehiclesView vehiclesView = db.VehiclesViews.Find(id);
if (vehiclesView == null)
{
return HttpNotFound();
}
return View(vehiclesView);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "VehicleId,Year,Name,Model,Type,EngineSize,Transmission,NumberOfDoors,Colour,Cost,Price,SoldDate")] VehiclesView vehiclesView)
{
if (ModelState.IsValid)
{
db.VehiclesViews.Add(vehiclesView);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(vehiclesView);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
VehiclesView vehiclesView = db.VehiclesViews.Find(id);
if (vehiclesView == null)
{
return HttpNotFound();
}
return View(vehiclesView);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "VehicleId,Year,Name,Model,Type,EngineSize,Transmission,NumberOfDoors,Colour,Price,Cost,SoldDate")] VehiclesView vehiclesView)
{
if (ModelState.IsValid)
{
db.Entry(vehiclesView).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(vehiclesView);
}
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
VehiclesView vehiclesView = db.VehiclesViews.Find(id);
if (vehiclesView == null)
{
return HttpNotFound();
}
return View(vehiclesView);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
VehiclesView vehiclesView = db.VehiclesViews.Find(id);
db.VehiclesViews.Remove(vehiclesView);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
What I have tried:
I am new to C# and even newer to Entity Framework. I have looked online to see if I can find a solution and what I have found so far is script to join the tables. However I am not sure if I need that considering I have done so in SQL