locked
MVC application creating a new table in existing database instead of updating the database RRS feed

  • Question

  • User-1744062909 posted

    Kris van der Mast: I updated the connection string as it contained both username and password. Please don't put such information on a public forum.

    I am creating an MVC application where I need to update an existing database.  Since I have never done this before I create a test application to help be understand the process.  I successfully connected to the database, however when I try to update the database, the application creates a new table instead.  I need some assistance with understanding why I am creating a new table instead of updating the existing table. 



    //*******Connection String <p><connectionstrings> <add name="WebApplication1Context" providername="System.Data.SqlClient" connectionstring="Server=.;Database=Blue;User=*****;Password=*******"></add> </connectionstrings></p> //****



    //********Model using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace WebApplication1.Models { public class CODE_SET { [Key] public string CODE_SET1 { get; set; } public DateTime ACTIVE_DT_TM { get; set; } public int ACTIVE_ID { get; set; } public DateTime BEG_EFFECTIVE_DT_TM { get; set; } public string DISPLAY { get; set; } public string DISPLAY_KEY { get; set; } public string DESCRIPTION { get; set; } public DateTime END_EFFECTIVE_DT_TM { get; set; } public DateTime UPDT_DT_TM { get; set; } public int UPDT_ID { get; set; } public int UPDT_CNT { get; set; } public int ACTIVE_TYPE_CD { get; set; } } } //******DBContext using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using WebApplication1.Models; namespace WebApplication1.Context { public class WebApplication1Context : DbContext { public DbSet<CODE_SET> CODE_SET { get; set; } } } //********Controller using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Data; using System.Data.Entity; using WebApplication1.Context; using WebApplication1.Models; namespace WebApplication1.Controllers { public class CODE_SETController : Controller { WebApplication1Context db = new WebApplication1Context(); // GET: CODE_SET public ActionResult Index() { //var codeSet = db.CODE_SET.ToList(); //return View(codeSet); return View(db.CODE_SET.ToList()); } // GET: CODE_SET/Details/5 public ActionResult Details(int id) { return View(); } // GET: CODE_SET/Create public ActionResult Create() { return View(); } // POST: CODE_SET/Create [HttpPost] public ActionResult Create(CODE_SET CODE_SET) { try { db.CODE_SET.Add(CODE_SET); db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(CODE_SET); } }


    Monday, July 30, 2018 10:22 PM

All replies