Click here to Skip to main content
14,122,675 members
Rate this:
 
Please Sign up or sign in to vote.
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();

        // GET: VehiclesViews
        public ActionResult Index()
        {
            return View(db.VehiclesViews.ToList());
        }

        // GET: VehiclesViews/Details/5
        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);
        }

        // GET: VehiclesViews/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: VehiclesViews/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [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);
        }

        // GET: VehiclesViews/Edit/5
        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);
        }

        // POST: VehiclesViews/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [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);
        }

        // GET: VehiclesViews/Delete/5
        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);
        }

        // POST: VehiclesViews/Delete/5
        [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
Posted
Updated 20-Apr-18 1:09am
Comments
F-ES Sitecore 20-Apr-18 5:23am
   
Rather than trying to update the view, act on the tables directly.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This is tricky, you need to understand more on base tables and updatable view, take look at below article to understand more on this situation..

sql - View or function '' is not updatable because the modification affects multiple base tables - Stack Overflow[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 20 Apr 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100