how to convert classes into database tables in asp.net mvc3

Unanswered how to convert classes into database tables in asp.net mvc3

  • 25 April 2012 6:22
     
     

    Hi Friends,

        I am new to asp.net_mvc3 and Json

    below is my code for json in mvc

     public class Default1Controller : Controller
        {
            sampleEntities db = new sampleEntities();
            
            public JsonResult Index()
            {
                IList<emp> emps = db.emps.ToList();
                return Json(emps, JsonRequestBehavior.AllowGet);

            }

    this works fine

    but my question if I create classes like below

      public class DatabaseContext : DbContext
        {
            public DbSet<Role> Roles { get; set; }//class Role

            public DbSet<Country> Countries { get; set; }//class Country

    I want these classes become my tables in database how can I do this

    If anybody knows the answer plz help me


    srujana

Semua Balasan

  • 13 Mei 2012 3:01
     
     

    Hi, 

    You can defining the table manually like this:

    [Table(Name = "mydb.dbo.nameOfMyTable")]
    public class nameOfMyTable
    {
         [Column(Name = "DbSet")]
         public DbSet<Role> Roles { get; set; }//class Role

         [Column(Name = "Countries")]
         public DbSet<Country> Countries { get; set; }//class Country

    }

    Hope it will help you.

    Best regards.