Mvc CodeFirst Aproach Using Entity Framework

Mvc CodeFirst Aproach Using Entity Framework

Introduction :

                 Here i am explain code first aproach in mvc4 using entityframework. With the Code-First approach, you can focus on the domain design and start creating classes as per your domain requirement rather than design your database first and then create the classes which match your database design

Step 1: First Create Mvc Web application

Step 2 : Now You can add model class called Blog.cs

 public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }

        public virtual List<Post> posts { get; set; }
    }

Step 3: now add Anothe class calles Post.cs

public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string PostDate { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

Step 4:  Now add BlogContext class this class can be inherited from the context class using this claas we can create the connection to the sql server. here we can use Dbset<> this attribute can used for access the model attribute.

    public class BlogController : Controller
    {
        BlogContext db = new BlogContext();
        // GET: /Blog/

        public ActionResult insert()
        {
            return View();
        }
        [HttpPost]
        public ActionResult insert( Blog bd)
        {
            BlogContext db = new BlogContext();
            db.Blogs.Add(bd);
            db.SaveChanges();
            return View();
        }

Step 5: Now you can add the connection string to web.config

 <connectionStrings>
    <add name="BlogContext" connectionString="server=.;database=CodeFirstDB ;integrated security=true;"  providerName="System.Data.SqlClient" />
  </connectionStrings>

Step: 6 Now add view  For ActionMethode "Insert" as following

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Blog</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

Step 7 : Now you run project and click on submit button the database can create as following

Mvc ModelFirst Aproach

Step 8: After Click on submit button Created database can see here






See Other Tutorial :

AngularJS CRUD Operation : Select Insert Edit Update and Delete using AngularJS in ASP.Net MVC

*  AngularJS With ASP.NET MVC

*  Convert Rows to columns using 'Pivot' in SQL Server

*  Mvc Registration page With user exist using Ajax method

*  MVC 4 How to Perform Insert Update Delete Edit Select Operation

*  MVC4 Edit,update,Delete,cancel inside gridview using sql database


*  MVC 4 Gridview To Display Data Using SQL Server Database With Simple code


*  Login page in asp.net Mvc4 Web application


*  Mvc4 How to bind Dropdown List using Sql Database


Gridview find control in asp.net
Previous
Next Post »