2.1: Entity Framework, References, Using Statements
Last updated
Last updated
In this module we'll address the errors caused by moving the IdentityModels.cs
file to a new assembly.
After moving IdentityModels.cs
to ElevenNote.Data you should be seeing some errors. These errors are a result of us needing certain references and packages for our solution. We'll add those items now. Currently, the code in the .WebMVC layer can't see the IdentityModels.cs
. It needs access. Also, the .Data layer does not have the Entity Framework package installed. IdentityModels.cs
needs that package to operate.
The first thing we'll correct is the assembly name that accompanies the IdentityModels.cs
file.
Go to the IdentityModels.cs
file.
Update the namespace to align with our current namespace:
Next, we'll install Entity Framework. Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. In this app, Entity Framework will help us connect to our database, and it will allow us to complete CRUD operations in our database.
Install EF by doing the following:
Right click on the solution and choose Manage Nuget Packages for Solution.
Note that it will sometimes default to the 'Installed' tab.
Click the browse tab. In the search bar, browse for EntityFramework.
Choose Microsoft.AspNet.Identity.EntityFramework.
Click Project.
Click Install.
Close the NuGet Package Manager.
We'll add a few references for our MVC project now. References, if you don't know, allow one assembly to access public classes in other assemblies. The web project needs to communicate with the other projects in our solution. As we shall soon see, it needs to know about the .Data layer, the .Services layer, and the .Models layer. This is done with references.
When adding references, it's smart to think with this formula:
"Does x assembly need to know about or use the y assembly?"
Does the web layer need to know about the service layer? Yes, the web layer will use the service to query the db.
Does the web layer need to know about the Data layer? Yes, the web layer needs the IdentityModels that we just moved.
Does the service layer need to know about the WebMVC layer? No, the service layer will have the duty of querying the db. It doesn't care where the query is coming from.
We'll work with this questioning as we operate.
Let's work with the references:
First, let's find the errors to fix by pressing CTRL + SHIFT + b
to build the project. If you don't see the Error Output at the bottom, go to the View tab and choose Error List
.
Use the Error List
to troubleshoot and navigate to errors.
You should see a list of errors which may vary slightly from the screenshot below:
Right-click on ElevenNote.WebMVC and go to Add -> Reference
Click on Projects -> Solution
Select the ElevenNote.Data, ElevenNote.Services & ElevenNote.Models projects and click OK. You should see this view:
Because we moved the IdentityModels.cs
file from the WebMVC project to the Data project, we need to bring in using statements for the Data project.
Again, pressCTRL SHIFT B
to build the project
You should have more errors - see below for approximation:
Double click on the first error to go to the code with the error
Click on the error in the file and CTRL .
to bring in the using statement at the top:
It should bring in the following using statement at the top of your file (your line #'s may differ)
CTRL SHIFT B
again to build the project. By the way,CTRL SHIFT B
saves everything first, and then builds a library for the project.
There should be more errors after building. To remove them, repeat the above process until all errors are gone.
Now might be a good time to back up your code: