I am just creating a
Student Class in Model folder with Some Properties in it where the complete code
of Student.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace TelerikMvcApplication3.Models
{
public class Student
{
[Key]
public int StudentId { get; set; }
[Required(ErrorMessage = "Please Enter FirstName")]
[StringLength(10, ErrorMessage = "FirstName morethan 10 charcs")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please Enter LastName")]
[StringLength(10, ErrorMessage = "LastName morethan 10 charcs")]
public string LastName { get; set; }
[Range(5, 50, ErrorMessage = "Age Should Be Between 5 and 50")]
public int Age { get; set; }
[Required(ErrorMessage = "Please Enter Location")]
[StringLength(10, ErrorMessage = "Location morethan 10 charcs")]
public string Location { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace TelerikMvcApplication3.Models
{
public class StudentStateEntities:DbContext
{
public DbSet<Student> Students { get; set; }
}
}
The Initializer class is also created to handle database or requirement changes for the application to maintain and handle situation. So the Complete code of StudentStateIntializer.cs looks like this. It's kept in Model's Folder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace TelerikMvcApplication3.Models
{
public class
StudentStateIntializer:DropCreateDatabaseIfModelChanges<StudentStateEntities>
{
protected override void Seed(StudentStateEntities context)
{
var stu = new List<Student>
{
new Student
{
StudentId=1,
FirstName="Vijay",
LastName="Prativadi",
Location="Banglore",
Age=25
},
new Student
{
StudentId=2,
FirstName="Uday",
LastName="Prativadi",
Location="Mumbai",
Age=28
}
};
stu.ForEach(p=>context.Students.Add(p));
}
}
}We will now go into global.asax and add a basic one line code to handle database changes for our intializer class:
Database.SetInitializer<StudentStateEntities>(new
StudentStateIntializer());
So the Complete Code of Global.asax looks like this:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using TelerikMvcApplication3.Models;
namespace TelerikMvcApplication3
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id =
UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
Database.SetInitializer<StudentStateEntities>(new
StudentStateIntializer());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
We are now adding Home Controller with Controller with read/write actions and
views using Entity Framework:





Join the conversation! Your thoughts help the community grow.