How to Update model and database with code first approach in Asp.net MVC

I'm new to mvc. I've creted an MVC app, in which i have used code first approach. Right now i have two tables <strong>Deal</strong> and <strong>Comment</strong>. Now i want to add a new table <strong>Category</strong> in the database and new column <strong>categoryId</strong> in <strong>Deal</strong> table.

How i can update database and model?

I'm using Sql Server 2008 R2 for Database.

<strong>I've following structure of class:</strong>

namespace FBWebApp.Models { public class Deal { public int ID { get; set; } // ID public string Title { get; set; } // Titolo del deal public string Description { get; set; } // Descrizione dell'annuncio public string FacebookUID { get; set; } // UID facebook dell'utente public string Visibility { get; set; } // Visibility public string Category { get; set; } public int Option1 { get; set; } public int Option2 { get; set; } public int Option3 { get; set; } public int Option4 { get; set; } public string PhotoURL { get; set; } // URL of the facebook photo profile public string Name { get; set; } // Name of the user public string ProfileUrl { get; set; } // URL of the facebook profile public string Photo1 { get; set; } // URL of the Photo1 (local ) public string Photo2 { get; set; } public string Photo3 { get; set; } public string Photo4 { get; set; } public string Photo5 { get; set; } } public class Comment { [Key] public int CommentId { get; set; } public string CommentText { get; set; } public int ID { get; set; } [ForeignKey("ID")] public Deal DelNav { get; set; } } public class DealDBContext : DbContext { public DealDBContext() : base("DealDBContext") { } public DbSet<Deal> Deals { get; set; } public DbSet<Comment> Comments { get; set; } } }


first add your model :

public class Category { public int ID { get; set; } public int cateName { get; set; } }

in Deal class :

public class Deal { //.. [ForeignKey("CatId")] public virtual Category Category { get; set; } }

after Enable Migration you should use this command in console manager to update your database :



Try using 'update-database -force -verbose' in the Package Manager Console.

If it doesn't work, modify the migration but typing 'add-migration somename' and it will apear in the Migrations folder.

If you are new to MVC and EF, definitely check out this tutorial. It explains all about that and everything else you need to know:



  • MySQL ERROR 1005: Can't create table (errno: 150)
  • json_encode for mysql query returns some null columns of some rows, but the columns aren't null
  • Accessing caller information quickly
  • Apache Cassandra schema design
  • I am having Directx 11 but WDDM 1.0 ? how to update it to WDDM 1.1? [duplicate]
  • Mysql insert query returns ERROR 1062 (23000): Duplicate entry '2147483647' for key '
  • visual studio fatal error LNK1120:
  • XMPPConnection Error in android
  • Should I optimize around reads or CPU time in Google App Engine
  • update record in database using jdatabase
  • How to calculate total across columns but one?
  • firebase, how to update data at a key
  • Attemping to change attributes of $(this) inside success function
  • rspec simple example getting error on request variable in integration test
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • QLPreviewController hide print button in ios6
  • Word Open XML Mail Merge
  • Android fill_parent issue
  • FileReader+canvas image loading problem
  • Pass value from viewmodel to script in zk
  • Using $this when not in object context
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Uncaught Error: Could not find module `ember-load-initializers`
  • Counter field in MS Access, how to generate?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory