In this video we'll be going through adding our database to our project.
So in the previous dude of downloaded and installed Microsoft SQL Server express if you didn't already
have it and you would have used it to design two tables one for the car and so the record on one for
the types of cars that's in the database.
So we want to know add the references to this database to our project and so we're going to go through
that by a step and a Microsoft makes it relatively easy to do that through the use of Ada net.
So we'll see exactly how that works.
So the first step is to go into your solution explore right click on the project and click add.
So we're adding a new item and then you go to the data categories to see visual see sharp items equal
to data and then we're choosing a deal putting it into two data models.
So that's the one that we're using.
And then I'm going to give it a name so the name I'm going to use is car and the B.
All right.
So the name of this data model is car into D.
And then I click add the next screen asks me the type of model that's I want so if I want the designer
from database into design or model empty code first model are good for us from database clicking on
each will give you a description.
But the default one that is selected is the one that I'm interested in.
And if you want to read up on what it does it says that is just going to create a model based on an
existing database.
It will settle the connection string and everything so that we can interact with the database through
this model.
So that's the one we want e f designer from database we click next and then we move on to a screen where
it wants us to choose our connection.
So I had built an application using North Wind in the past.
So it's hung up on that connection but that's not what I'm interested in right.
So I'm just going to go to new connection and then I'm going to type in the server name so remember
that your server name would be your P.C. name backslash escudos or Xia express or other.
But a shortcut could be just to put that dot the backslash and see askew Express.
So I'm dot is equivalent to your machine name pretty much in this situation.
And then leaving that there you can go down to the part where you select or enter a database name you
drop this down and you'll see all of the databases that you have on the ESC express.
I have to or you may have few because you have these default ones or maybe you've been using it from
before but the one you're interested in is car rental because that's the one that we're using for this
project.
So I select car and so I can test the connection just to make sure everything is okay and then I click
okay to proceed.
Once that is done you see that the con takes care of changes and it showing you the connection string
that is going to be generated.
And I'm going to actually just walk you through what you're looking at in this section because it looks
confusing but it's really simple.
Once you know what you're looking for.
So the meta data section is just seeing that.
Okay we're adding This is like some stuff to declare the model that will be added.
That's auto generated you know that's up the wearable that the major parts of it though would be the
part that says connection string is equal it's a data source so data source is talking about the server
you're looking at.
So when I say a server you're looking at right now we're looking at our local server our own P.C. with
the school express database instance is hosting our database in the event that this database was not
on your P.C. then this would not be the same value.
This would be that maybe the IP or the machine name off that other computer where that database is some
going through this to tell you that if you're developing this in an enterprise setting where you know
you you have your P.C. and the database is on another P.C. somewhere else in the company you need to
connect your application to that central database then the data source would be the name of the machine
or the IP address to that particular machine.
All right.
The Qatar love talks about the database that you are connecting to and then well integrated security
dichotomy into multiple active results.
That is very important for multiple persons to be able to connect to the database simultaneously.
But as you can see those are all already there and they're already set to true.
So the two parts I want to focus on they are the data source and the initial catalogue.
Once again data sources the name or the address of the machine that has the database and then in catalogue
would be the database in an enterprise setting.
You would be probably shut down by security as in access to that central database would be metered by
security.
So in other situations you your went up a user idea and a password inside the connection string to be
able to connect.
But that's for a later date.
So right now we have it local.
We have our connection string.
It will be saved in the fire app config so you can see config is one of the files that we've got with
our project.
So that will be modified to have this connection string but we can click next and then we can choose
the latest into different work.
That's fine.
We can next again.
Now this screen is going to ask us which objects do we want to import into our model from the database.
So if I drop down into tables I'm going to see all of the tables listed in the database which is all
of the two that we just created so I can just tick tables.
So if you add more tables or three or using another database of all the tables you want all the tables
your tables you want all of your views likely views you want all of his third procedures you take them
whatever is in the database.
This entity framework model is able to import it and put it in the context of your project.
All right.
So I want to take tables and I'll leave these default settings.
Notice the name of the namespace car into a model.
That's fine.
The click finish and then it will go ahead and do its magic so once he's finished doing the loading
and creation then we see our models.
So let me just take some time to explain exactly what you're looking at here.
If you're familiar with database design then you're familiar with diagrams and one of the diagrams that's
very important would be the entity relationship diagram.
So the integer relationship diagram shows like a visual representation of your table the fields in there
maybe the data types and to some extent and also any relationships between each table.
Each table is an entity and then you have the relationships between them hence the name entity a relationship
diagram.
Now if you're looking at this and you're familiar with what the yard looks like then you'd notice that
this looks just like any the.
I have the two entities in my database are the two tables and I see the relationship between them.
When I click this line is going to highlight that the idea in types of cars is related to type of car
I.D..
And it also shows you that you can directly access the data instead of type of car through that relationship.
We'll get into that later but the point is that the model is giving you direct access to the tables
within the context of C sharp.
So once you have added this model and you reach this far I encourage you to do A build and that build
can be done using the keyboard shortcuts control shift and b or you can just go to build and build solutions.
So it's the same thing but you're not actually starting the obligation.
You don't need to start application.
You just wanted to control a shift and B and let everything recompile so that everything knows that.
Okay.
No the database connection is there.
All right.
So once we have seen that we have a successful build then we can proceed with the rest of our development
before it's in a court I want to just walk you through this diagram and what you need to know.
So I'm going to right click in the empty space click properties and then I'm going to say all right
in the properties we have two very important names one more imports on the other.
So we have entity container name which is called car into entities and then we have namespace called
car rental model.
So whenever we need to access our data models here we need to know all these two names are.
When that time comes you will see however another important thing that's I want to note.
Let's go to the solution explorer and then you'll see that some references got added and you would see
the new file got added and you'll see that there also class files with the same names as the tables.
So these class files were actually generated per table.
So you see car into the record but yes when you click on it you look at it.
It was also generated when you look at it it looks just like a C sharp plus.
So the point is that when we want to get data or insert data or manipulate data we're literally just
doing all of it to an interface called the links which will look at later but an interface called the
link that will interact with these more need to see sharp glasses and once they're interacting with
them we're actually interacting with our database.
So a dual use is a nice little interface to connect to our database without actually needing to worry
about a secure and other types of leaks that would be needed for that kind of operation but we can keep
everything homogenous and everything looking like C sharp code no.
The last thing that we're going to do in this video is actually very data related and that is to insert
some default value so that when we start hooking up to the database we don't have any problems.
So notice that we need to specify the type of car from our farm.
So what we want to do eventually is to bind.
Let me go to the design we want to bind the items that's our in this list to what is in the database.
So we went through the manual step of thinking this car it's going to edit items and inserting these
what I want to do is have these in the database or whether more than these are fewer than these but
these values I don't want them hardcoded because if the Honda is 0 tomorrow I want to be able to control
it from the database instead of having to come into the design every time and change that option.
Right.
So how much are you going to clean these out from here from this election.
Okay.
And then I'm going to go over my database and then I went to insert these values into that table sir.
Click on tables they expand.
I click on types of cars right click and go to edit top two hundred rows so later on we'll actually
develop an interface that allows us to manage the types of cars in the database but Forno just humor
me and type them in manually.
So we have Honda we have Lada we have buggy and Toyota.
Those were the values that I had in my farm for the types of cars.
So we have Honda Lada buggy and Toyota.
Those are the types of cars in our database.
So in our next video we will actually come back to our code behind and run a query that retrieves the
types of cars and then binds it to the list of items available inside of or humble box and then we'll
go ahead and see how we can collect data and submit to the database.