Featured Articles

Decoration

All Stories

Thursday, 15 September 2022

Connect To Ms SQL Server with Visual Studio 2022 | Ef Code First Approach

 In this article i will show you how you can  can connect to a ms sql server database using EF core or Entity Framework core code first approach in visual studio 2022. This will tell how to connect to ms sql server database using EF core by code first approach. To mode the changes to db i have used add-migration command. and update migration command to commit the changes.


First create a new asp.net core 6 mvc application and install below mention packages.

Packages to install: ------------------------------ Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.SqlServer

Now we will add connection string in appsettings.json file.

  "ConnectionStrings": {

    "myconn": "Server=.\\SQLEXPRESS;Database=SalesDB;Trusted_Connection=True;"

  }

 

After this we will create a contetxt class file and add the below code in a folder



using Microsoft.EntityFrameworkCore;

 

namespace CodeFirst.DBContext

{

    public class SalesDBContext : DbContext

    {

        public SalesDBContext(DbContextOptions options) : base(options)

        {

        }

        public DbSet<SalesProducts> SalesProducts { get; set; }

    }

} 


 Now we will create a class class file which will have table fields.


using System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations.Schema;

 

namespace CodeFirst.DBContext

{

    public class SalesProducts

    {

        [Key]

        public int Id { get; set; }

        [Column("PrductName", TypeName = "Varchar(200)")]

        public string ProductName { get; set; }

        public int Qty { get; set; }

        public int SalesCount { get; set; }

    }

}

 

Now open program.cs file and add the below code to register DB context class file.

var provider = builder.Services.BuildServiceProvider();

var configuration = provider.GetRequiredService<IConfiguration>();

builder.Services.AddDbContext<SalesDBContext>(item => item.UseSqlServer(configuration.GetConnectionString("myconn"))); 

var app = builder.Build();

 

Always put your code above highlighted part of code.  Now run the below command to create migration and create the DB.


To add a migration

-----------------------------

add-migration MigrationName update-database To remove specific migration --------------------------------------------- Update-Database -Migration LastMigrationName To remove all migration --------------------------------------- Update-Database -Migration 0 To remove last migration ------------------------------------------ Remove-Migration -force

One you done with executoin you get a migration folder.


Now check your SQL server DB.


9/15/2022 - By Pranav Singh 0

Monday, 12 September 2022

Asp net Core 6 MVC Connect to MS SQL Database Using EF Core Code First Approach | Visual Studio 2022

 In this article i will show you how you can  can connect to a ms sql server database using EF core or Entity Framework core code first approach in visual studio 2022. This will tell how to connect to ms sql server database using EF core by code first approach. To mode the changes to db i have used add-migration command. and update migration command to commit the changes.


First create a new asp.net core 6 mvc application and install below mention packages.
Packages to install: ------------------------------ Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.SqlServer

Now we will add connection string in appsettings.json file.

  "ConnectionStrings": {

    "myconn": "Server=.\\SQLEXPRESS;Database=SalesDB;Trusted_Connection=True;"

  }

 

After this we will create a contetxt class file and add the below code in a folder



using Microsoft.EntityFrameworkCore;

 

namespace CodeFirst.DBContext

{

    public class SalesDBContext : DbContext

    {

        public SalesDBContext(DbContextOptions options) : base(options)

        {

        }

        public DbSet<SalesProducts> SalesProducts { get; set; }

    }

} 


 Now we will create a class class file which will have table fields.


using System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations.Schema;

 

namespace CodeFirst.DBContext

{

    public class SalesProducts

    {

        [Key]

        public int Id { get; set; }

        [Column("PrductName", TypeName = "Varchar(200)")]

        public string ProductName { get; set; }

        public int Qty { get; set; }

        public int SalesCount { get; set; }

    }

}

 

Now open program.cs file and add the below code to register DB context class file.

var provider = builder.Services.BuildServiceProvider();

var configuration = provider.GetRequiredService<IConfiguration>();

builder.Services.AddDbContext<SalesDBContext>(item => item.UseSqlServer(configuration.GetConnectionString("myconn"))); 

var app = builder.Build();

 

Always put your code above highlighted part of code.  Now run the below command to create migration and create the DB.


To add a migration

-----------------------------

add-migration MigrationName update-database To remove specific migration --------------------------------------------- Update-Database -Migration LastMigrationName To remove all migration --------------------------------------- Update-Database -Migration 0 To remove last migration ------------------------------------------ Remove-Migration -force

One you done with executoin you get a migration folder.


Now check your SQL server DB.


9/12/2022 - By Pranav Singh 0

Friday, 9 September 2022

Asp.net Core 6 MVC Connect to MS SQL Database EF Core Using C# | Asp.Net Core EF Database First


 In this article i will show you how to connect asp.net core 6 mvc project or application with ms sql server database using entity framework core or EF core and c#. In this i have explained the DB first approach or database first approach or db first approach. I have used scamfold-dbcontext command.

First you need to install below mention nugget packages.

  1.          Microsoft.EntityFrameworkCore.Tools
  2.          Microsoft.EntityFrameworkCore.Design
  3.          Microsoft.EntityFrameworkCore.SqlServer

After this we need to run the below scamfold-dbcontext context in package manager console. Here the below  mention command we will run if we are running the command first time.


Scaffold-DbContext "Server=.\SQLEXPRESS;Database=Employee;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir data

 

In above need to define the connection string and then provider and then defined the directory where we EF core context and table object class files will create.

To update the DB context after making change in Database table. We will run below command.


Scaffold-DbContext "Server=.\SQLEXPRESS;Database=Employee;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir data –force


In above we have added –force. So by using this we will be able to update the context file and modified table objects class files.


Related Topics:

- Entity Framework Database First In ASP.NET Core

- Entity Framework Core with Existing Database

- EF Core 6 Database First / DB First (Entity Framework Core 6)

- How do I create a database first in Entity Framework?

- How do I create a DbContext in Entity Framework core database first?

- EF Core Database-First Tutorial for .NET Core

- Creating a model for an existing database in entity

- EF core database first sql server

- .Net core database first

- Entity framework database first

- ASP.Net Core: Entity Framework Database First Approach

- Database First approach in Entity Framework Core 6

- Database first approach in entity framework core 6


DOWNLOAD


9/09/2022 - By Pranav Singh 0

Friday, 26 August 2022

Asp.Net Core 6: Login Form Using Ms SQL C# | Simple Login Using Entity Framework in Asp.net Core MVC

 In this article I will show you how you can create a login page in asp.net core mvc / asp.net core 6 web application from ms sql database using c#.net.

So first we will create a new asp.net core application. After creating app we will create a new model class file and add the below code.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks; 

namespace EmployeeManager.Models

{

    public class EmpoyeeModel

    {

        public string LoginId { get; set; }

        public string Pasword { get; set; }

    }

}

 

Now we will install below mention packages.




Run the below mention command in package manage for entity frame work DB connection.


Scaffold-DbContext "Server=.\SQLEXPRESS;Database=Employee;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force

 

Now at your controller end we will add the below  HTTP get code in your controller.

   [HttpGet]

        public IActionResult Index()

        {

            EmpoyeeModel _empoyeeModel = new EmpoyeeModel();

            return View(_empoyeeModel);

        }

 

After this we will create the view and add the below code.


@model EmployeeManager.Models.EmpoyeeModel

@{

    ViewData["Title"] = "Home Page";

}

<div class="container">

    <div class="d-flex justify-content-center h-100">

        <div class="card">

            <div class="card-header">

                <h3>Sign In</h3>

                <div class="d-flex justify-content-end social_icon">

                    <span><i class="fab fa-facebook-square"></i></span>

                    <span><i class="fab fa-google-plus-square"></i></span>

                    <span><i class="fab fa-twitter-square"></i></span>

                </div>

            </div>

            <div class="card-body">

                @using (Html.BeginForm("Index", "Home", FormMethod.Post))

                {

 

                    if (ViewBag.LoginStatus != null)

                    {

                        if (ViewBag.LoginStatus == 0)

                        {

                            <div class="alert alert-danger">Invalid login detail</div>

                        }

                    }

                    <div class="input-group form-group">

                        <div class="input-group-prepend">

                            <span class="input-group-text"><i class="fas fa-user"></i></span>

                        </div>

                        @Html.TextBoxFor(m => m.LoginId, new { @class = "form-control", @placeholder = "username" })

 

                    </div>

                    <div class="input-group form-group">

                        <div class="input-group-prepend">

                            <span class="input-group-text"><i class="fas fa-key"></i></span>

                        </div>

                        @Html.PasswordFor(m => m.Pasword, new { @class = "form-control", @placeholder = "password" })

                    </div>

                    <div class="row align-items-center remember">

                        <input type="checkbox">Remember Me

                    </div>

                    <div class="form-group">

                        <input type="submit" value="Login" class="btn float-right login_btn">

                    </div>}

            </div>

            <div class="card-footer">

                <div class="d-flex justify-content-center links">

                    Don't have an account?<a href="#">Sign Up</a>

                </div>

                <div class="d-flex justify-content-center">

                    <a href="#">Forgot your password?</a>

                </div>

            </div>

        </div>

    </div>

</div>

 

Now we will create code for validating user from DB.


  [HttpPost]

        public IActionResult Index(EmpoyeeModel _empoyeeModel)

        {

            EmployeeContext _employeeContext = new EmployeeContext();

            var status = _employeeContext.EmployeeLogin.Where(m => m.LoginId == _empoyeeModel.LoginId && m.Password == _empoyeeModel.Pasword).FirstOrDefault();

            if (status == null)

            {

                ViewBag.LoginStatus = 0;

            }

            else

            {

                return RedirectToAction("SuccessPage", "Home");

            }

            return View(_empoyeeModel);

        }

 

Now we will create the success action method. So this action method we are crating to redirect user after successful login.


<h1>

    Success Page after login

</h1>

 

Now check the DB table.




Now  run the application and check the output.

 



8/26/2022 - By Pranav Singh 0

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top