Friday, October 4, 2019

How to Connect SQL Server Database in Asp.net Core (db First Approach EF-CORE) (Part-3)


Note : For Beginner Guide Articles :

https://dotnetcorecommunity.blogspot.com/p/aspnet-core-tutorials-paracticals.html


Step#1 : Watch Previous Article to Install EF-CORE in asp.net core project  and here is the table script which we are going to use in coming articles.

--------------------------------------------------------------------------------------------------------
CREATE TABLE [dbo].[tbl_User](
[u_id] [int] IDENTITY(1,1) NOT NULL,
[u_email] [nvarchar](50) NOT NULL,
[u_password] [nvarchar](50) NOT NULL,
[u_UserName] [nvarchar](50) NOT NULL,
[u_ProfilePicture] [nvarchar](50) NULL,
[u_AddedDate] [datetime] NULL,
[u_ModifiedDate] [datetime] NULL,
[u_isuserlogin] [int] NULL,
[u_isuseraproved] [int] NULL,
PRIMARY KEY CLUSTERED 
(
[u_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
[u_email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
--------------------------------------------------------------------------------------------------------


https://dotnetcorecommunity.blogspot.com/2019/10/how-install-entity-framework-core-in.html

now check your model folder which doesnot contain any model class right now except of ErrorviewModel


Scaffold-DbContext Command: 

step#2 : Goto Tools--> packge manager Console  and write this command 

Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>] 
                    [-DataAnnotations] [-Force] [-Project] [-StartupProject] [<CommonParameters>]
im placing . for server means current PC server ,database name is DB_SOCIALAPP userid is salman and password is *****
so my command  would be like ....

Scaffold-DbContext "Server=.;Database=DB_SOCIALAPP;user id=salman;password=*****" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models


now check the model folder , db Context class and other entities has been created .



now, goto the appsettings.json file and  add the connectionstring as show below :

 "ConnectionStrings": {
    "Testdb": "Server=.;Database=DB_SOCIALAPP;user id=salman;password=12345"
  }





the next you need to do is go to the startup.cs file and paste the following code in configure service method.

  var ConnectionString = Configuration.GetConnectionString("Testdb");

            //Entity Framework
            services.AddDbContext<DB_SOCIALAPPContext>(options => options.UseSqlServer(ConnectionString));



Testdb is the name of connection string which I have set in appsettings.json and my dbcontextclass name is DB_SOCIALAPPContext , you could have the different file name ,so change it as per your context class name.

add this namespace  (using Microsoft.EntityFrameworkCore;) in statup.cs file .

build the project!

now create a Controller , Right Click on controller ---->  MVC controller with view Entity framework



select model class and db context from these dropdowns


don't forget to rename the controller name  , I renamed it to userController from tbluserController .

now again go to Startup.cs to change the mvc route .


change controller name . Now build and run ... you are ready to perform Crud operation in asp.net Core ,In next blog we will create our own empty controller and views.


Previous Lesson                                                                                                Next Lesson

1 Comments:

At October 10, 2019 at 10:01 PM , Blogger Unknown said...

Very beneficial to see this blog.

 

Post a Comment

Do not Add Spam links in the Comment Box

Subscribe to Post Comments [Atom]

<< Home