patterncsharpMinor
Master detail INSERT in Entity Framework 6 Database First MVC 5
Viewed 0 times
detailinsertmvcdatabasemasterfirstframeworkentity
Problem
I have a DB with a master table called "facturas" and another detail table "facturas_detalle." I would like to insert to them, so this is a "Database-First".
I need some guidance or advise of best practice here. I've read some examples that are outdated versions of Entity Framework and ASP with no MVC approach.
I already created a model (using the assistant) from the database and I have stored procedures to write to them. Using the Store Procedure Mapping from VS 2015 I mapped the entities to those store procedures for Insert. I didn't marked pluralize option because some other tables names are uppercase and aren't nouns.
I also generated a controller and views for the "facturas" table, and it creates successfully entries in DB. How should I edit the view and controller to Insert one or more
facturas_pruebaController.cs:
```
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Abaco_ASP.Models;
namespace Abaco_ASP.Controllers
{
public class facturas_pruebaController : Controller
{
private PAQAEntities db = new PAQAEntities();
// GET: facturas_prueba/Create
public ActionResult Create()
{
ViewBag.almacen = new SelectList(db.CATALMA, "COD_ALM", "NOM_ALM");
ViewBag.cliente = new SelectList(db.CATCTES, "COD_CTE", "NOM_CTE");
ViewBag.usuario = new SelectList(db.FACPARU, "cod_usu", "cod_Alm");
ViewBag.plaza = new SelectList(db.PLAZAS, "PLAZA", "LAST_COD_CTE");
return View();
}
// POST: facturas_prueba/Create
// To protect from overposting attacks, please enable the specific properties you want to bind
I need some guidance or advise of best practice here. I've read some examples that are outdated versions of Entity Framework and ASP with no MVC approach.
**facturas**
folio PK
fecha
almacen FK
cliente FK
plaza FK
usuario FK
id_factura
----------
**facturas_detalle**
folio FK
articulo FK
cantidad
precioI already created a model (using the assistant) from the database and I have stored procedures to write to them. Using the Store Procedure Mapping from VS 2015 I mapped the entities to those store procedures for Insert. I didn't marked pluralize option because some other tables names are uppercase and aren't nouns.
I also generated a controller and views for the "facturas" table, and it creates successfully entries in DB. How should I edit the view and controller to Insert one or more
facturas_detalle in the same page? Is this approach correct?facturas_pruebaController.cs:
```
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Abaco_ASP.Models;
namespace Abaco_ASP.Controllers
{
public class facturas_pruebaController : Controller
{
private PAQAEntities db = new PAQAEntities();
// GET: facturas_prueba/Create
public ActionResult Create()
{
ViewBag.almacen = new SelectList(db.CATALMA, "COD_ALM", "NOM_ALM");
ViewBag.cliente = new SelectList(db.CATCTES, "COD_CTE", "NOM_CTE");
ViewBag.usuario = new SelectList(db.FACPARU, "cod_usu", "cod_Alm");
ViewBag.plaza = new SelectList(db.PLAZAS, "PLAZA", "LAST_COD_CTE");
return View();
}
// POST: facturas_prueba/Create
// To protect from overposting attacks, please enable the specific properties you want to bind
Solution
Be wary of
It's preferable to leverage strong typing of models in a view. However. To the best of my knowledge, you can only bind a single model to a view, so you'll need to use partial views to make the magic happen.
There is more that can be said about the code though.
ViewBag. It's convenient, but if you misspell something, you'll never know it. No compiler error or warning. No runtime exception. Nothing. The object just never gets passed to the view and no element gets rendered. I just recently ran into a bug caused by this in one of my own applications, so I know just how difficult it can be to even spot that something was ever wrong to begin with. I only spotted it as a fluke. It's preferable to leverage strong typing of models in a view. However. To the best of my knowledge, you can only bind a single model to a view, so you'll need to use partial views to make the magic happen.
There is more that can be said about the code though.
- I like that you're verifying model state before posting.
- I don't care for the
Createaction that does nothing but return a view. I would expect it to be theIndexaction.
- I would be inclined to keep your db context constrained to a
usingblock instead of overriding the controller'sDisposemethod.
- I would also be inclined to introduce a factory class for the db context and a constructor that takes an instance of that factory, or rather it's interface, in as an argument. Moving to dependency injection would allow you to unit test the controller (and you really should be unit testing).
Context
StackExchange Code Review Q#107692, answer score: 3
Revisions (0)
No revisions yet.