Web
Analytics
Custom Login Using Entity Framework in asp.net MVC using Razor | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Step 1: First Create new Project in Visual studio.

MVC New Project

Step 2: create new asp.net web application

CreateNewASP.NET MVC Project




Step 3: Create new MVC Empty Project

New MVC Empty Project

 

Step 4: First We will write connection string into web.config. In this example I have created a database named MVCEx and a table Login into it.

Database Table

 
  

Step 5: Install Entity Framework into project using package manager console.

PackageManagerConsole




Step 5: Write shell command to install Entity Framework

install-package entityframework

Entity Framework Installation Command

Step 6 : Now create two classes into Models folder , In this example I created two classes named DataContext.cs and Login.cs and write code.

DataContext.CS

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Custom_Login_EF.Models
{

//Ineherit properties of DbContext Class which is availabe in using //System.Data.Entity; namespace
 public class DataContext : DbContext
 {

//IF we does not specify connection string with name as //DataContext then be have to call parent class //constructor(base("conn")) in datacontext constructor.
 public DataContext() : base("conn") { }

//DbSet is used to do all sql operation in designated table 

public DbSet Logins { get; set; }
 }
}

Login.CS

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Custom_Login_EF.Models
{
 [Table("Login")]
 public class Login
 {
 [Key]
 public int id { get; set; }
 public string UserName { get; set; }
 public string Password { get; set; }
 }
}

Step 7: Create new Controller Home write code like below.

using Custom_Login_EF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Custom_Login_EF.Controllers
{

public class HomeController : Controller
 {
 //
 // GET: /Home/
 public ActionResult Index()
 {

return View();
 }
 [HttpPost]
 public ActionResult Index(Login obj)
 {
 DataContext db = new DataContext();
 var output = db.Logins.FirstOrDefault(m => (m.UserName == obj.UserName) && (m.Password == obj.Password));
 if (output != null)
 {
 ViewBag.msg = "Success full Login";
 }
 else
 {
 ViewBag.msg = "Un Success full Login";
 }

return View();
 }
 }
}




Step 8 : Create Index.cshtml in View Folder

@model Custom_Login_EF.Models.Login
@{
 ViewBag.Title = "Index";
 Layout = null;
 string msg = "";
}
@if (ViewBag.msg != null)
{
 msg = ViewBag.msg;
}

Index

@using (Html.BeginForm()) {

@if (msg != null) { @Html.Label(msg); }

User Name@Html.TextBoxFor(m => m.UserName)
Password@Html.PasswordFor(m => m.Password)

}

Step 9: Now run and see results

LoginScreen




 

Download Complete Source Code