Generate C# Model from existing Microsoft SQL Server Database

Hacked
10 Jan 202311:50

Summary

TLDRThis tutorial video guides viewers on generating code-first models from an existing MSSQL database within a C# project using Visual Studio 2019. It begins by setting up the necessary tools, including the latest Visual Studio and SQL Server, and installing the .NET workload. The presenter demonstrates how to connect to a database, create a new one if needed, and add a sample 'products' table with attributes like product ID, name, price, and description. After updating the database, the video proceeds to show how to create a new console application, add an ADO.NET Entity Data Model, and generate code-first models from the database. Finally, it illustrates how to access and print data from the database using Entity Framework, providing a practical example of integrating a database with a C# application.

Takeaways

  • 😀 The video provides a tutorial on generating code-first models from an existing MSSQL database in a C# project using Visual Studio 2019.
  • 🛠️ Prerequisites include having the latest version of Microsoft Visual Studio, SQL Server, and the .NET workload installed via the Visual Studio installer.
  • 🖥️ The tutorial starts by opening Visual Studio 2019 and using the Server Explorer to connect to a SQL database.
  • 🔗 It demonstrates how to add a new data connection using the SQL client with Windows authentication and how to create a new database if one does not exist.
  • 📝 The video shows step-by-step instructions to create a new table named 'products' with columns for Product ID, Name, Price, and Description.
  • 🔄 After creating the table, the tutorial explains how to update the database schema and verify the changes by refreshing the table view.
  • 📈 The presenter adds sample data to the 'products' table and shows how to view the data using the Server Explorer.
  • 💡 The next part of the tutorial involves creating a new Console Application project in .NET Framework, not .NET Core, to differentiate the two.
  • 🔧 The process of adding an ADO.NET Entity Data Model to the project is detailed, with the selection of the database and the tables to be included in the model.
  • 📑 The video shows the automatic generation of the Entity Framework DbContext and entity classes like 'Product.cs' that correspond to the database table structure.
  • 🔍 Finally, the tutorial concludes with a simple example of how to retrieve and print data from the 'products' table using a for-each loop in the Console Application.

Q & A

  • What is the main topic of the video?

    -The video is about generating code-first models from an existing MSSQL database within a C# project using Visual Studio 2019.

  • What are the prerequisites before starting the process shown in the video?

    -The prerequisites include having the latest version of Microsoft Visual Studio, SQL Server, and the .NET workload installed using the Visual Studio installer.

  • How does one open the Server Explorer in Visual Studio 2019?

    -To open the Server Explorer, you can go to the 'View' menu and select 'Server Explorer' from there.

  • What is the first step in creating a connection to a database in the video?

    -The first step is to right-click on 'Data Connections' and then select 'Add Connection'.

  • Which SQL client is used in the video?

    -The SQL client used in the video is SQL Server Express.

  • What type of authentication is used to connect to the database in the video?

    -Windows authentication is used to connect to the database in the video.

  • How is a new database created in the video?

    -A new database is created by entering a new database name (e.g., 'ytt111') and clicking 'OK', which prompts to create the database if it does not exist.

  • What is the name of the table created in the video?

    -The table created in the video is named 'products'.

  • What are the attributes added to the 'products' table in the video?

    -The attributes added to the 'products' table are 'Product ID', 'Name', 'Price', and 'Description'.

  • How is the database schema updated after creating the table?

    -The database schema is updated by clicking on 'Update Database' in the Server Explorer.

  • What is the purpose of creating a new console app project in the video?

    -The purpose of creating a new console app project is to demonstrate how to generate code-first models from the database using Entity Framework.

  • What framework is used for the console app project in the video?

    -The .NET Framework is used for the console app project in the video, not .NET Core.

  • How is the Entity Data Model added to the project?

    -The Entity Data Model is added by right-clicking on the project, selecting 'Add New Item', and choosing 'ADO.NET Entity Data Model'.

  • What is the method used to generate the code-first model from the database?

    -The code-first model is generated by selecting 'Generate Code First from database' in the Entity Data Model Wizard.

  • What is the name of the entity class created for the 'products' table?

    -The name of the entity class created for the 'products' table is 'Product.cs'.

  • How are the data from the 'products' table retrieved and displayed in the console app?

    -The data is retrieved using a foreach loop that iterates over the 'products' entity set and prints out the product name and price.

Outlines

00:00

😀 Setting Up the Environment for Code Generation

This paragraph introduces the video's purpose, which is to demonstrate how to generate code-first models from an existing MSSQL database within a C# project using Visual Studio 2019. The presenter advises viewers to subscribe to the channel and outlines the prerequisites: having the latest version of Microsoft Visual Studio, SQL Server, and the .NET workload installed. The presenter then shows how to open Visual Studio, access the Server Explorer, and add a new data connection using SQL Server. They proceed to create a new database named 'ytt' and add a 'products' table with columns for product ID, name, price, and description. The table is updated, and the presenter shows how to view and add data to the 'products' table.

05:01

😀 Creating a New Project and Entity Data Model

The presenter starts by creating a new console application project in Visual Studio using the .NET Framework. They then guide the viewer on how to add a new item to the project, specifically an ADO.NET Entity Data Model named 'entity'. The goal is to generate a code-first model from the database. The presenter selects the database 'yt111' created earlier and tests the connection. They proceed to select the database tables to import and finalize the model generation. The result is an Entity Framework DbContext and an automatically generated 'product.cs' file that corresponds to the 'products' table in the database, with properties matching the table's columns.

10:05

😀 Implementing Data Access and Displaying Results

In the final paragraph, the presenter focuses on implementing data access within the console application. They show how to use a foreach loop to iterate over the 'products' entity set retrieved from the database context. The presenter writes code to print the product name and price to the console, converting the price to a string for display. The video concludes with a demonstration of running the application, which successfully prints the product data from the database. The presenter encourages viewers to explore and build upon the provided example and reminds them to subscribe to the channel for more upcoming content.

Mindmap

Keywords

💡Visual Studio 2019

Visual Studio 2019 is an integrated development environment (IDE) from Microsoft. It is essential for creating and managing software projects, particularly for generating code-first models from an existing SQL database in a C# project. In the video, Visual Studio 2019 is used to demonstrate the setup and management of the project.

💡SQL Server

SQL Server is a relational database management system developed by Microsoft. It is used for storing and retrieving data as requested by other software applications. In the video, SQL Server is the database system from which the code-first models are generated.

💡.NET Framework

.NET Framework is a software framework developed by Microsoft that provides a controlled environment for developing and running applications. It supports the creation of applications using C#. In the video, the presenter opts to create the console application using the .NET Framework instead of .NET Core.

💡Entity Framework

Entity Framework (EF) is an open-source object-relational mapping (ORM) framework for ADO.NET. It enables developers to work with a database using .NET objects. The video demonstrates how to generate code-first models using Entity Framework from an existing database.

💡Code-First Model

Code-First Model is an approach in Entity Framework where the database schema is generated from the code. The video focuses on generating code-first models from an existing SQL database, showing how to set up and configure the database context and entities in a C# project.

💡ADO.NET Entity Data Model

ADO.NET Entity Data Model is a part of the Entity Framework, which allows developers to create a conceptual model of their data. In the video, the presenter adds an ADO.NET Entity Data Model to the project to facilitate the generation of code-first models.

💡Server Explorer

Server Explorer is a tool in Visual Studio that provides access to server resources, including databases. In the video, it is used to add a connection to the SQL Server database and manage database objects like tables.

💡Database Connection

A database connection is a connection to a database through which SQL commands and queries can be executed. The video shows how to set up a connection to the SQL Server database using Visual Studio's Server Explorer.

💡Console Application

A console application is a program that runs in a command-line interface or terminal. In the video, a console application project is created in Visual Studio to demonstrate how to generate and use code-first models from a SQL database.

💡Data Annotations

Data Annotations are attributes that can be applied to classes and properties to enforce validation rules and define database schema details. In the video, the presenter creates entity classes with properties that correspond to the database table columns, implicitly using data annotations.

Highlights

Introduction to generating code first models from an existing MSSQL database in a C# project using Visual Studio 2019.

Requirement of having the latest version of Microsoft Visual Studio and SQL Server installed.

Installation of the .NET workload through the Visual Studio installer.

Opening Visual Studio 2019 and Server Explorer to connect to the database.

Adding a new data connection and selecting the SQL client.

Entering server details and using Windows authentication for database connection.

Creating a new database if it doesn't exist within the SQL Server.

Adding a new table 'products' with attributes like product ID, name, price, and description.

Updating the database schema with the new table and attributes.

Viewing and adding sample data to the 'products' table.

Creating a new Console Application project in .NET Framework.

Adding a new ADO.NET Entity Data Model to the project.

Generating the code first model from the database using Entity Framework.

Selecting the database and tables to import into the Entity Data Model.

Automatic generation of the Entity Framework DbContext and entity classes.

Creation of a 'Product.cs' entity class that corresponds to the 'products' table.

Using a for-each loop to retrieve and print product data from the database.

Demonstration of running the application to display data from the database.

Encouragement to subscribe to the channel for more upcoming videos.

Transcripts

play00:01

hello everyone

play00:03

welcome back once again

play00:05

in this video we'll look at how we can

play00:07

generate code first models from an

play00:09

existing msql database inside the

play00:13

c-sharp project using visual studio 2019

play00:17

but before we get started please make

play00:19

sure

play00:20

you subscribe to the channel if you

play00:22

haven't

play00:23

so let's get to it then

play00:25

so to continue these are the things that

play00:28

are required obviously you should have

play00:30

the latest version of Microsoft

play00:32

Visual Studio

play00:34

and then SQL Server

play00:36

you have to also make sure that you

play00:38

install the.net workload using the

play00:41

visual studio installer so once you've

play00:43

got all these sets then

play00:45

we open the visual studio right so as

play00:48

you can see I've I've actually got a

play00:49

visual studio 2019 opened and then I've

play00:52

got the start Windows I'm just going to

play00:54

close it yeah so if you look at it here

play00:56

I've got the server Explorer open as

play00:58

well so if you don't have it open you

play01:01

can click on The View and if you find

play01:02

the server Explorer here right so once

play01:06

we've actually got that a bit yeah then

play01:07

we can right click on the data

play01:09

connections right

play01:11

select and right click the data

play01:13

connections and then add a connection so

play01:16

here

play01:17

obviously you can select your data

play01:19

source okay in my case I'm selecting the

play01:21

SQL client right and then you should you

play01:24

have to enter your

play01:26

server name in my case it's a DOT

play01:29

backslash SQL Express

play01:31

and then the log details yeah in my case

play01:34

also is a Windows authentication right

play01:37

and then here

play01:40

you can enter the database that you want

play01:42

to use if you don't have it you can

play01:44

enter a new one and create it right in

play01:46

my case I'm just going to create a new

play01:48

one so I'm just going to add ytt

play01:51

one one so if we click OK yeah

play01:59

you see so the database does not exist

play02:02

so if we click on yes then we should get

play02:06

a new one created for us so we click on

play02:07

yes

play02:10

and then we've got a new database

play02:12

created yeah so here we've actually got

play02:14

nothing inside the table so we're just

play02:16

going to add one table quickly right so

play02:19

add a new table

play02:26

so here the table we're just going to

play02:28

change the table name right

play02:32

and we're just going to call it products

play02:35

yeah

play02:38

so here we're just going to add some few

play02:41

details here so the idea we're just

play02:43

gonna make it

play02:46

product ID

play02:51

and then

play02:53

name for product name so the

play02:56

data type we're just going to make it

play02:58

test

play03:01

and then uh

play03:04

we're just gonna put

play03:07

price and the data type we're just gonna

play03:10

make it money

play03:12

right

play03:13

so as you can see it's not very

play03:16

difficult so we're just going to add one

play03:18

more attribute description

play03:23

so the description we're gonna make a

play03:26

test as well

play03:27

yeah so as you can see here we've got a

play03:30

very simple stuff here so this is how we

play03:32

actually update the database right we

play03:34

click on this update

play03:41

so now we can click update database here

play03:51

so as you can see update completed

play03:54

successfully so if we come here refresh

play03:57

the table

play03:59

so as you can see we've got a new table

play04:01

inside

play04:02

product with all the attributes yeah

play04:05

so this is what we we can also do so we

play04:07

right click on the table product right

play04:09

and then show table data

play04:13

right so here as you can see we've got

play04:16

nothing inside so we can just add

play04:20

data to the table

play04:32

so I will just

play04:42

we're just gonna add some few data so

play04:45

I'm just going to post it while I do so

play04:47

yeah so as you can see here I've

play04:49

actually added a couple of

play04:52

a couple of days at the table so you can

play04:54

just refresh it yeah

play04:57

and everything should be fine yeah so

play05:00

the next stage

play05:01

yeah the next stage this is what we're

play05:03

actually going to do so I'm just going

play05:05

to close this bit here so as you can see

play05:08

here I've got an empty solution with

play05:10

nothing inside so I'm just gonna click

play05:12

on the the start window

play05:14

and then creates a new project

play05:19

so here I'm just going to type in

play05:21

console app

play05:24

so the console lab this time here we're

play05:26

actually going to use the.net framework

play05:29

not the.net core yeah because the the

play05:32

net call I'll make a separate video on

play05:34

that right

play05:35

so we click on the.net framework and

play05:38

then next

play05:40

so here you can configure the project

play05:42

details so we're not going to look to to

play05:44

that

play05:45

so we're just going to wait for the

play05:47

project to create

play05:49

so as you can see now we've actually got

play05:51

a console app created now

play05:54

so what we do is right click on the

play05:57

project and then add new item

play06:03

so here we click on the data and then

play06:07

ado.net entity data model right

play06:10

so here we're just going to call this

play06:12

one entity

play06:21

call this entity

play06:24

and then here this is what we're going

play06:25

to do we're actually going to generate

play06:28

the code first from database yeah so

play06:30

here you can also generate it from an

play06:33

Entity framework designer model

play06:35

oh from um and empty code first model

play06:39

right so this is what we're gonna do so

play06:41

we're going to generate the code first

play06:42

model from the database right so now we

play06:45

click on the nest

play06:47

so from here we can actually select our

play06:49

database so if we if you may recall

play06:53

uh the the the connection string and

play06:55

whatnot yeah so now we can actually

play06:58

browse the data of the databases and

play07:02

select the one we actually created right

play07:04

so yt111 yeah so we can test the

play07:07

connection

play07:08

the connection is successful

play07:11

so you can click on OK

play07:14

so now as you can see here we've

play07:16

actually got it selected here so we can

play07:18

click on the nest

play07:20

so here we can select whatever

play07:22

attributes of that database that we want

play07:24

to import

play07:25

in this case we're just going to import

play07:28

a DB or just for the database tables

play07:30

right

play07:31

so once you actually select that then we

play07:33

click on the Finish

play07:37

so it should take a while then so I'm

play07:40

just going to pause it

play07:42

so as you can see here now we've

play07:44

actually got the Entity framework DB

play07:46

contest and everything generated for

play07:48

that for us automatically right

play07:50

so if we look here as you can see we've

play07:53

also got product.cs created this is

play07:56

actually in line with the product table

play07:58

that we actually created

play08:00

here so if we look at it

play08:03

so we've got all the necessary

play08:05

properties inside the table

play08:08

so if we look at the type name is test

play08:10

the money if we look at it when we

play08:12

actually created the database

play08:17

the database what all the stuff that we

play08:19

actually added yeah so

play08:21

as you can see here now it's not it's

play08:24

not it's not difficult so we've got the

play08:26

entity class so we can actually up grab

play08:28

the data that we actually added inside

play08:30

the database by doing

play08:34

um

play08:36

entity

play08:39

okay but we're going to use the using

play08:46

entity

play08:51

DB contest security

play08:55

new entity right

play09:01

so now we could say

play09:04

DB

play09:07

dot products

play09:11

dots

play09:13

to list

play09:17

that for each so we can just look

play09:19

through the products

play09:22

P for each product

play09:26

console

play09:29

the right line

play09:31

so we could just say

play09:44

okay so I'm just gonna make it simple

play09:46

I'm just gonna remove the whole thing

play09:48

here and actually use the for each Loop

play09:56

yeah so for each variable

play10:00

products

play10:04

in

play10:08

DB

play10:12

dot products

play10:15

then we can write console

play10:21

the right line

play10:25

so now we can actually

play10:29

print out

play10:31

so the product name this will be

play10:34

P dot name

play10:39

and the price will be

play10:41

something like

play10:44

product dot price

play10:47

so we actually convert this to string as

play10:50

well

play10:52

so put a small letter to C

play10:55

foreign

play11:02

I'm just gonna leave it just like that

play11:05

that's the price right and then

play11:12

console

play11:14

the read line

play11:18

so right now we're just going to run it

play11:19

to see what actually comes out of here

play11:23

so as you can see here now we've

play11:25

actually got the the data from the

play11:27

database

play11:28

table printed out here now so I'm just

play11:31

going to leave it here for you to

play11:32

explore it forever so you can just look

play11:34

at it and just try and build it on

play11:36

yourself so once again yeah if you

play11:39

haven't subscribed to the Channel please

play11:40

make sure you do because I've got lots

play11:42

of videos coming up and I hope this

play11:44

actually video helps you

play11:46

and peace

Rate This

5.0 / 5 (0 votes)

Related Tags
Code-FirstC#Visual StudioSQL DatabaseEntity FrameworkDatabase Design.NET FrameworkModel GenerationVideo TutorialProgramming Guide