Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900