Now we need to add the below mention Nuget Package from our package manager.
Install-Package
Microsoft.EntityFrameworkCore.SqlServer -Version 5.0.13 |
Note: you need to install package
as per your need. You can get the Asp.Net Core packages from https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer
After
installing the SQL server package. You will get all packages list install in dandies
in solution explorer as shown below:
After this
we will install Entityframework tool.
Now select the appropriate package version an install it.
After this
we will install package to generate entity table object. For this we again need
to install the package for entityframework. To perform this action we need to
run the below command in package manager.
Scaffold-DbContext
"Server=.\SQLExpress;Database=Test;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data |
Here just
check the highlighted name “Data” this is folder where all your entity table object class will be created. Now check
the folder which you have created in root of the project. Check your Data folder
for entity table object.
Now in our project we will add model class.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace WebApplication1.Models { public class LoginModel { public string LoginId
{ get; set;
} public string Password
{ get; set;
} } } |
After
creating project we will add controller in your controller
folder. In this asp.net core controller folder we will add a new
controller named as "HomeController.cs".
[HttpGet] public IActionResult
Index() { LoginModel
loginModel = new LoginModel(); return View(loginModel); } |
Now we will
create our view and add the below code.
@model WebApplication1.Models.LoginModel @{
ViewBag.Title = "Login form"; } @using (Html.BeginForm("Index", "Home")) {
<span style="font-weight:bolder; font-size:16px; color:red;">@ViewBag.Message</span>
<div> <h2>User
Login</h2> <div class="row"> <div class="form-group col-12"> <lable for="loginid">Login Id</lable> @Html.TextBoxFor(m => m.LoginId, new { @class = "form-control" }) </div> </div> <div class="row"> <div class="form-group col-12"> <lable for="loginid">Password</lable> @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) </div> </div> <div class="row"> <div class="form-group col-12"> <input type="submit" value="Submit" /> </div> </div>
</div> } |
In above View code I have imported model class and bind it to the Html helper controls. So for binding the TextBoxFor control I have used lemda expression and bind it will model class property. This will help us to get the user added value in textbox and password control.
Now we will
create the post method and add the below code. This piece of code will help us
to get the value from view to controller.
[HttpPost] public IActionResult Index(LoginModel loginModel) { TestDBContext testDBContext = new TestDBContext(); var status = testDBContext.UserLoginMasters.Where(m => m.LoginId
== loginModel.LoginId && m.Password ==
loginModel.Password).FirstOrDefault(); if (status != null) { ViewBag.Message = "Success full login"; } else { ViewBag.Message = "Invalid login detail."; } return View(loginModel); } |
Now we will check by running the code. First we will add wrong detail and check the response. In break point we can check that the added value we are getting in model.
As we have added a wrong detail. So we will get invalid detail message.
Now here is the final output. As user enter correct detail he will get below success message.
In your
case you need to make changes in DB connection string check the code. This
piece of code is present in Context file. Which Is there in data folder.
protected override void
OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer("Server=\\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;"); } } |
Download (Source Core)
Download(DB Script)
0 comments:
Please let me know your view