Lab Answer Key Module 7 - Accessing A Database
Lab Answer Key Module 7 - Accessing A Database
Exercise 1: Creating an Entity Data Model from The School of Fine Arts Database
Es
te
do
Task 1: Build and cgenerate
um an EDM by using a table from the SchoolGradesDB database
No en
to
es pe
tán r
pe ait tene
rm urra ce a
1. Start the MSL-TMG1 itid virtual
@ machine if it is not already running.
as se An
las rnat drés
co u Itu
2. Start the 20483A-SEA-DEV11-07 pia r.cvirtual
l rmachine.
ra
ss Ur
in qu
au iet
3. Log on to Windows® 8 as Student with tor the password a. Pa$$w0rd.
iza
ció
n.
Note: If necessary, click Switch User to display a list of users in the Windows 8 logon screen.
Es
4. Switch totethe
do Windows 8 Start window.
cu
me
5. No e Studionto2012.
Click Visual pe
stá r
np ait tene
6. erm urr ce
In Visual Studio, onitidthe aFile
@ menu,a A point to Open, and then click Project/Solution.
as s ern ndré
las a sI
7. In the Open Project dialog cobox,tubrowse tuto
rra E:\Labfiles\Starter\Exercise 1, click GradesPrototype.sln, and then
pia r.cl
click Open. s sin Ur
qu
au iet
tor a.
iza
8. On the File menu, point to Add, and thencclick ión New Project.
.
9. In the Add New Project dialog box, in the templates list, click Class Library, in the Name box, type
Grades.DataModel, and then click OK.
15. In the Connection Properties dialog box, in the Server name box, type (localdb)\v11.0, in the Select or enter
a database name list, click SchoolGradesDB, and then click OK.
Es
te Data Model Wizard, on the Choose Your Data Connection page, click Next.
16. In the Entitydo
cu
me
N nto
17. On theoChoose
es Your pDatabase
ert Objects and Settings page, expand Tables, expand dbo, select the
tán e
following tables, and then click Finish:
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 1/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
• Grades
• Students
• Subjects
• Teachers Es
te
do
cu
• Users me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ aA
a
18. If the Security Warning s ern box
s la dialog nd appears, click Do not show this message again, and then click OK.
sc atu rés I
op r. tur
ias cl ra
Ur
19. On the Build menu, click Build Solution.
sin qu
au iet
tor a.
iza
ció
n.
1. Es
In the designer
te window, review the entities that have been generated.
do
cu
me
2. Review nto and navigation properties of the Grade entity.
No the properties
es pe
tán r
p ait tene
3. Right-click theeheading
rm u ce Grade entity, and then click Table Mapping.
r of the
itid ra@ aA
as s e n
4. las rnat drés
In the Mapping Details – Grade co pane,
u Ireview
tur the mappings between the columns in the database table and
pia r.cl ra
the properties of the entity. s s Ur
in qu
au iet
tor a.
5. In Solution Explorer, expand GradesModel.edmx, i za expand GradesModel.Context.tt, and then double-click
ció
n.
GradesModel.Context.cs.
6. In the code window, note that the wizard has created a DbContext object named SchoolGradesDBEntities.
Exercise 2: Updating Student and Grade Data by Using the Entity Framework
Es
te
do
cu
me
No
Task 1: Display gradesntfor
o p the current student
es ert
tán e
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 2/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
1. In Visual Studio, on the File menu, point to Open, and then click Project/Solution.
2. In the Open Project dialog box, browse to E:\Labfiles\Starter\Exercise 2, click GradesPrototype.sln, and then
click Open.
3. In Solution Explorer, right-click GradesPrototype, and then click Set as StartUp Project.
Es
Task 2: Display
te the subject name in the UI
do
cu
me
No nto
es pe
tán rte
1. In Visual Studio, in the Task List window, double-click the TODO: Exercise 2: Task 2a: Convert the subject ID
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 3/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
2. In the code editor, click in the blank line below the comment, and then type the following code:
5. In the code editor, click in the blank line below the comment, and then type the following code:
return
E subject.Name != string.Empty ? subject.Name : "N/A";
ste
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ aA
as s e nd
las rnview
Task 3: Display the GradeDialog rés use the input to add a new grade
atu and
co Itu
pia r.cl rra
ss Ur
in qu
au iet
tor a.
1. In the Task List window, double-click the i zaTODO: Exercise 2: Task 3a: Use the GradeDialog to get the details
ció
of the new grade. task. n.
2. In the code editor, click in the blank line below the comment, and then type the following code:
Es
GradeDialog gd = new GradeDialog();
t ed
oc
um
No en
to
es pe
In the TasktáList r
3. np ait tedouble-click
window, n the TODO: Exercise 2: Task 3b: Display the form and get the details of
erm urr ece
the new grade. task.itid a@ a
as se An
las rnat drés
4. In the code editor, click incthe ur.
op blank Itu
ias cl line rbelow
ra
Ur
the comment, and then type the following code:
sin qu
au iet
tor a.
iza
if (gd.ShowDialog().Value) ció
n.
{
5. Click in the blank line below the final TODO comment in this try block, and then type the following code:
Es
te
do
cu
} me
No nto
es pe
tán rte
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 4/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
6. In the Task List window, double-click the TODO: Exercise 2: Task 3c: When the user closes the form, retrieve
the details of the assessment grade from the form and use them to create a new Grade object. task.
7. In the code editor, click in the blank line below the comment, and then type the following code:
SessionContext.DBContext.Grades.Add(newGrade);
SessionContext.Save();
Es
te
do
c
10. In the Task Listumwindow,
en double-click the TODO: Exercise 2: Task 3e: Refresh the display so that the new
No to
es
grade appears. task. pe
tán r
pe ait tene
rm u rra ce a
11. In the code editor,iticlick
da at @ the endA of the comment, press Enter, and then type the following code:
s la sern ndré
sc at s
op ur.cl Iturra
ias Ur
Refresh(); sin qu
au iet
tor a.
iza
ció
n.
7. In the New
Es Grade Details dialog box, in the Subject list, click Geography, in the Assessment box, type
te
do
A+, in the Comments box, type Well done!, and then click OK.
cu
me
No nto
8. Verify that pe is added to the list, and then close the application.
es the new grade
tán rte
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 5/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
Results: After completing this exercise, users will be see the grades for the current student and add new
grades.
Es
te
oc d
Exercise 3: Extending
um the Entity Data Model to Validate Data
No en
to
e stá pe
r
np ait tene
erm urr ce
Task 1: Throw the ClassFullException exception
itid a@ a
as se And
las rnat rés
co u Itu
pia r.cl rra
1. In Visual Studio, on the File menu,ss Ur
in point to Open, qu
iet
and then click Project/Solution.
au a.
tor
iza
2. In the Open Project dialog box, browse to ciE:\Labfiles\Starter\Exercise
ón 3, click GradesPrototype.sln, and then
.
click Open.
3. In Solution Explorer, right-click GradesPrototype, and then click Set as StartUp Project.
4.
Es Explorer, right-click Grades.DataModel, point to Add, and then click Class.
In Solution
te
do
5. In the Add New cu Item – Grades.DataModel dialog box, in the Name box, type customTeacher.cs, and then
me
No nto
click Add.
es pe
tán r
pe ait tene
u
rm modify r ce
6. In the code editor, itid ra@the aclass An declaration as shown in the following code:
as s e
las rnat drés
co u Itu
pia r.cl rra
public partial class Teacher s sin Ur
qu
au iet
tor a.
iza
ció
n.
7. In the code editor, in the Teacher class, type the following code:
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 6/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
}
// Verify that the student is not already enrolled in another class.
if (student.TeacherUserId == null)
{
// Set the TeacherID property of the student.
student.TeacherUserId = UserId;
}
Este
else
do
cu
{ me
No nto
es // If the pestudent is already assigned to a class, throw an ArgumentException.
tán r
p ait tene
throw ce
erm newurrArgumentException("Student", "Student is already assigned to a
itid a@ aA
class"); a s
s la ern n d
sc at rés
} op ur.cl Iturra
ias Ur
} sin qu
au iet
tor a.
iza
ció
n.
9. In the Task List window, double-click the TODO: Exercise 3: Task 1a: Call the EnrollInClass method to assign
the student to this teacher’s class. task.
10. In the code editor, click in the blank line below the comment, and then type the following code:
Es
te
do
cu
me
No nto
SessionContext.CurrentTeacher.EnrollInClass(student);
es pe
tán r
pe ait tene
rm u r ce
itid ra@ aA
s
as double-click
e n
11. In the Task List window, las rnat drés the TODO: Exercise 3: Task 1b: Save the updated student/class
co u Itu
information back to the database. pia r.ctask.
l rra
ss Ur
in qu
au iet
12. In the code editor, click in the blank line tor below the a. comment, and then type the following code:
iza
ció
n.
SessionContext.Save();
Es
te
do
cu
me
Novalidation nto
Task 2: Add es logic
pe for the Assessment and AssessmentDate properties
tán r
pe ai tene
rm turra ce a
itid @
as se And
l as rnatGrades.DataModel,
1. In Solution Explorer, right-click ré point to Add, and then click Class.
co ur. s Itu
pia c l rra
ss Ur
2. In the Add New Item – Grades.DataModel in qu
au iet box, in the Name box, type customGrade.cs, and then
dialog
tor a.
click Add. iza
ció
n.
3. In the code editor, modify the class declaration as shown in the following code:
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 7/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
5. In the code editor, below the existing using directives, type the following code:
Es
te
do
cu
using me
System.Text.RegularExpressions;
No nto
es pe
tán r
pe ait tene
rm u r ce
itid ra@ aA
6. In the code editor, inathe s
s la Grade nd
ern class, type the following code:
sc atu rés I
op r. t urr
ias cl aU
sin rqu
public bool ValidateAssessmentGrade(string au iet
tor a. assessment)
iza
{ ció
n.
// Verify that the grade is in the range A+ to E-.
// Use a regular expression: A single character in the range A-E at the start of
the string followed by an optional + or - at the end of the string.
Match matchGrade = Regex.Match(assessment, @"^[A-E][+-]?$");
E
ifste(!matchGrade.Success)
doc
{ um
No en
to
e
st// If thepegrade
r is not valid, throw an ArgumentOutOfRangeException.
án
p ait tene
e
throw u c
rm new rrArgumentOutOfRangeException("Assessment",
ea "Assessment grade must be
itid a@ An
in the range A+ ato s la s
E-"); e d
r r
s c natu és It
} op r.c urr
ias l aU
s in rqu
else au iet
tor a.
{ iza
ció
return true;
n.
}
}
Es
te
7. In the Task dList
oc window, double-click the TODO: Exercise 3: Task 2a: Create a Grade object. task.
um
No en
to
8. es editor, click
In the code pe in the blank line below the comment, and then type the following code:
tán rte
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 8/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
9. In the Task List window, double-click the TODO: Exercise 3: Task 2b: Call the ValidateAssessmentDate
method. task.
10. In the code editor, click in the blank line below the comment, and then type the following code:
Es
te
do
um c
testGrade.ValidateAssessmentDate(assessmentDate.SelectedDate.Value);
e
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@
11. In the Task List window, a
as double-click
se And the TODO: Exercise 3: Task 2c: Call the ValidateAssessmentGrade.
r ré
las na
task. co tur s Itu
pia . cl rra
ss Ur
i n qu
12. In the code editor, click in the blankauline below the
iet comment, and then type the following code:
tor a.
iza
ció
n.
testGrade.ValidateAssessmentGrade(assessmentGrade.Text);
Es
te
do
cu
Task 3: Run the me
application and test the validation logic
No nto
es pe
tán r
pe ait tene
rm u r ce
itid ra@ aA
1. On the Build menu, aclick s
s la Build nd
ern Solution.
sc atu rés I
op r.c tur
l
ias Without ra
2. On the Debug menu, click Start sin Debugging.
Ur
qu
au iet
tor a.
3. When the application loads, in the Username i za
ció box, type vallee, and in the Password box, type
password99, and then click Log on. n.
5. In the Assign Student dialog box, click Eric Gruber, in the Confirm message box, click Yes, and then in the
Es
Error enrolling
te student message box, click OK.
do
cu
me
6. In theNAssign
oe nto dialog box, click Close.
Student
stá pe
r
np ait tene
7. In the Class 3C erm
view,urclick
r ce
Kevin Liu, and then click Add Grade.
itid a@ a
as se And
8. In the New Grade Details las rnat rés
codialogu box,Itin
pia r.cl urrthe Date box, type tomorrow’s date, and then click OK.
ss aU
i n rqu
9. In the Error creating assessment amessage uto iet click OK.
box, a.
riz
ac
10. In the New Grade Details dialog box, in the iónDate box, type 8/19/2012, in the Assessment box, type F+,
.
and then click OK.
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 9/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database
Results: After completing this exercise, the application will raise and handle exceptions when invalid data is
entered.
Es
te
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ a
as se And
las r na ré
co tur s Itu
pia . cl rra
ss Ur
in qu
au iet
tor a.
iza
ció
n.
Es
te
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ aA
as s e n
las rnat drés
co u Itu
pia r.cl rra
ss Ur
in qu
au iet
tor a.
iza
ció
n.
Es
te
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ a
as se An
las rnat drés
co u Itu
pia r.cl rra
ss Ur
in qu
au iet
tor a.
iza
ció
n.
Es
te
do
cu
me
No nto
e stá pe
n rte
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 10/10