Understanding how to load and retrieve information from a database using a controller using ASP.NET.
I am currently working on a project where I need to retrieve data from a database and order and sort it.
Examining the Generated Code: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller
I have never done this before. And I have been advised to look at the documentation tutorial.
Upon reviewing it I fail to understand the process. Is Moviecontext a super object that we pass properties and abilities to it through inheritance and value assignment?
Do we use properties to assign capabilities?
And than also how is the information passed to the view and ordered?
Do we use DbSet<Entity> properties to add capabilites to the object we are trying to pass to the view?
DbContext options configure the DdbBontext object.
Textbook example
in this example its not loading anything.
Accessing Your Model's Data from a New Controller
In this section, you'll create a new
MoviesController class and write code that retrieves the movie data and displays it in the browser using a view template.39 Replies
Yes, you use
DbSets to access data. Think of them as a representation of tables
Not sure what you mean by "capabilities" hereAfter reviewing the textbook I believe these are the methods for learning how to search the database for information.
Three ways to select a movie by its id int id = 1;
var movie = context.Movies.Where(m => m.MovieId == id).FirstOrDefault(); var movie = context.Movies.FirstOrDefault(m => m.MovieId == id); var movie = context.Movies.Find(id);
All correct, yes
According to the textbook configuration options powers are given to this object like the ability to restrict txt length and other database restrictions
Yes, you can configure how those tables end up generated
Okay so I I want to access the database and grab information from it I create an object called context
Thats where we make it possible to create an object that has DbContext abilities
Ill do this step by step.
Thats where we make it possible to create an object that has DbContext abilities
Here is where we pass configuration objects.
So is there were we say load up the movies database table in its entirety or just the column.
Neither is loaded here
The DbContext just describes the database, and DbSets the tables
Nothing is loaded until you explicitly load it
okay how do i actually do that
like this, for example
um were you going to show an example?
It's in the message I replied to
all I am seeing is like this, for example
But, sure
No linked message above?

oh wow! NO Wonder I was confused!
I see this part
So basically there is a series of instructions for what data needs to be organized and how, and than .ToList(); executes the command to actually grab the data!
.ToList() among others, yes
.First(), .Count(), .FirstOrDefault(), and probably a few others also do that
Generally, whatever method does not return an IQueryable<T> probably resolves the querytyping one sec
So we take the time to create a class called Movie Context. and dpass it DbContext abilities and DbContexOptions
And than.... where does the context keyword get involved?
What do you mean by "context keyword"?
In your example we assign it these powers?
You could call it many things, but not "assigning powers" lmao
but the keyword context comes from no where. There is no class that instatnated it
Ah, so you're asking about dependency injection then
Yes that word keeps getting thrown around.
For all intents and purposes, all you need to know that it's a magic box of stuff, and anything in that box can access anything else in that box
So if you have two classes registered in a DI container, say
you can use the constructor to tell
Bar to grab Foo from the box:
The DI container will create an instance of Foo
And just pass it to Barmy textbook shows me this
is what adds your
MovieContext into the DI box
Then, sometime later, you should have something like .AddMvc() or .AddRazorPages() or something like that
That's what adds your controllers to the box
So now, the controllers can tell the box to give them the contextSo the DI container creates an object named Foo.
And than the class of Bar creates an ojbect named Bar but when it does, it grabs the created Foo object and gains its methods an attributes.
The
Bar class is also managed by the DI container
But yes, that's basically itCan you explain the DI container? what is that?
I know I am asking a lot of questions thank you for your patience.
The magic box of stuff
Really, it's easier when you just think about it as such
So depdency injection, put simply is when the DI creates an object that another class will use when creating its objects.
Yep
So where was context born?
Do you offer tutoring services in C# you seem to know your stuff.
The DI container instantiated it
And no, I don't do private tutoring
darnit
okay so I need to learn about the DI container from the documentation. see how it created context. context can than as a super object grab all of the infomration in a database table and than we use LINQ methods to retrieve information from that object.
man my textbook is worthless. okay i need to research that.
@Angius thank you I will look into this and than try to access data. If I have more questions i should just keep asking them here correct?
Generally, DI is simply nothing you worry about
You just put things in the box, and request them from the box
this would be pulling things from the box.
@Angius I cannot thank you enough all of this has been a challenge for me. I am trying my best to become a programmer and escape customer service.
:Ok:
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View