How to add entity framework in visual studio
In this tutorial, we are going to use our first MVC application which we had create over here How to create MVC project in visual studio
Now we have our MVC application ready with us, now we will go ahead to add entity framework into our application.
Steps in this tutorial:
1) Open newly created MVC project
2) Install Entity Framework
3) Create Tables in database
4) Add Entity ADO.NET Model in Model
5) Generate entity classes for the database tables
We are going to use DatabaseFirst approach over here instead of CodeFirst approach or Model First Approach.
In DatabaseFirst approach, we have to create create database tables first then EF will automatically generate entity C# classes automatically.
In ModelFirst approach we have to create UML diagrams first, then EF will generate domain classes and tables for us.
In CodeFirst approach, we have to write down the C# entity domain classes first then these classes are used to create the database tables automatically by EF.
In our databaseFirst approach we will create some tables, then we will create entity for these tables.
We will create a table called tblLogin in our Demo database.
CREATE TABLE [dbo].[tblLogin]( [Id] [int] IDENTITY(1,1) NOT NULL, [username] [varchar](10) NOT NULL UNIQUE, [password] [varchar](50) NOT NULL, [IsActive] [bit] NULL, [sid] [varchar](16) NOT NULL, [count] [int] NULL, PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
In order to this, Click on tools, then select NuGet Package Manager, then select package manager console:
Then in console type below command:
PM> install -package EntityFramework
Now we have entity framework installed, now we will go ahead and will add ADO.NET Entity Data model
Now we will give name to our newly created entity data model as EntityDataModel and click on ok.
Now select EF designer from database:
Now we will create new connection:
Now Click on Finish.
Congrats! Your Entity Data Model has been created successfully!
Now you can create encrypted login system using this entity classes. How to do that? Please check out below tutorial:
How to create encrypted login password using C# MVC
Comments
Post a Comment