使用Entity Framework和WCF Ria Services开发SilverLight之1:简单模型
本文目的是通过Silverlight ria service完成一次数据的读取过程,并且在此基础上建立测试项目。
Ria service借助于WCF和ADO.NET Entity Framework构建分布式开发框架。使用它可以快速构建自己的开发模式。
1:基础结构
首先,创建SL APP,如下:
然后,选择创建web:
可以,也可以不勾选enable wcf ria service,如果勾选了,在SL APP中会多几个DLL的引用,其它没有任何差别。如图:
2:创建一个示例数据库
这是一个小而轻型的数据库,在MDSN的课程中有使用到它。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO USE [master]; GO IF EXISTS ( SELECT * FROM sys.databases WHERE name = 'School' ) DROP DATABASE School; GO -- Create the School database. CREATE DATABASE School; GO -- Specify a simple recovery model -- to keep the log growth to a minimum. ALTER DATABASE School SET RECOVERY SIMPLE; GO USE School; GO -- Create the Department table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[Department]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[Department]( [DepartmentID] [ int ] NOT NULL , [ Name ] [nvarchar](50) NOT NULL , [Budget] [money] NOT NULL , [StartDate] [datetime] NOT NULL , [Administrator] [ int ] NULL , CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ( [DepartmentID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO -- Create the Person table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[Person]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[Person]( [PersonID] [ int ] IDENTITY(1,1) NOT NULL , [LastName] [nvarchar](50) NOT NULL , [FirstName] [nvarchar](50) NOT NULL , [HireDate] [datetime] NULL , [EnrollmentDate] [datetime] NULL , CONSTRAINT [PK_School.Student] PRIMARY KEY CLUSTERED ( [PersonID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO -- Create the OnsiteCourse table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[OnsiteCourse]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[OnsiteCourse]( [CourseID] [ int ] NOT NULL , [Location] [nvarchar](50) NOT NULL , [Days] [nvarchar](50) NOT NULL , [ Time ] [smalldatetime] NOT NULL , CONSTRAINT [PK_OnsiteCourse] PRIMARY KEY CLUSTERED ( [CourseID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO -- Create the OnlineCourse table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[OnlineCourse]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[OnlineCourse]( [CourseID] [ int ] NOT NULL , [URL] [nvarchar](100) NOT NULL , CONSTRAINT [PK_OnlineCourse] PRIMARY KEY CLUSTERED ( [CourseID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO --Create the StudentGrade table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[StudentGrade]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[StudentGrade]( [EnrollmentID] [ int ] IDENTITY(1,1) NOT NULL , [CourseID] [ int ] NOT NULL , [StudentID] [ int ] NOT NULL , [Grade] [ decimal ](3, 2) NULL , CONSTRAINT [PK_StudentGrade] PRIMARY KEY CLUSTERED ( [EnrollmentID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO -- Create the CourseInstructor table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[CourseInstructor]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[CourseInstructor]( [CourseID] [ int ] NOT NULL , [PersonID] [ int ] NOT NULL , CONSTRAINT [PK_CourseInstructor] PRIMARY KEY CLUSTERED ( [CourseID] ASC , [PersonID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO -- Create the Course table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[Course]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[Course]( [CourseID] [ int ] NOT NULL , [Title] [nvarchar](100) NOT NULL , [Credits] [ int ] NOT NULL , [DepartmentID] [ int ] NOT NULL , CONSTRAINT [PK_School.Course] PRIMARY KEY CLUSTERED ( [CourseID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO -- Create the OfficeAssignment table. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[OfficeAssignment]' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[OfficeAssignment]( [InstructorID] [ int ] NOT NULL , [Location] [nvarchar](50) NOT NULL , [ Timestamp ] [ timestamp ] NOT NULL , CONSTRAINT [PK_OfficeAssignment] PRIMARY KEY CLUSTERED ( [InstructorID] ASC ) WITH (IGNORE_DUP_KEY = OFF ) ON [ PRIMARY ] ) ON [ PRIMARY ] END GO -- Define the relationship between OnsiteCourse and Course. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_OnsiteCourse_Course]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[OnsiteCourse]' )) ALTER TABLE [dbo].[OnsiteCourse] WITH CHECK ADD CONSTRAINT [FK_OnsiteCourse_Course] FOREIGN KEY ([CourseID]) REFERENCES [dbo].[Course] ([CourseID]) GO ALTER TABLE [dbo].[OnsiteCourse] CHECK CONSTRAINT [FK_OnsiteCourse_Course] GO -- Define the relationship between OnlineCourse and Course. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_OnlineCourse_Course]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[OnlineCourse]' )) ALTER TABLE [dbo].[OnlineCourse] WITH CHECK ADD CONSTRAINT [FK_OnlineCourse_Course] FOREIGN KEY ([CourseID]) REFERENCES [dbo].[Course] ([CourseID]) GO ALTER TABLE [dbo].[OnlineCourse] CHECK CONSTRAINT [FK_OnlineCourse_Course] GO -- Define the relationship between StudentGrade and Course. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_StudentGrade_Course]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[StudentGrade]' )) ALTER TABLE [dbo].[StudentGrade] WITH CHECK ADD CONSTRAINT [FK_StudentGrade_Course] FOREIGN KEY ([CourseID]) REFERENCES [dbo].[Course] ([CourseID]) GO ALTER TABLE [dbo].[StudentGrade] CHECK CONSTRAINT [FK_StudentGrade_Course] GO --Define the relationship between StudentGrade and Student. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_StudentGrade_Student]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[StudentGrade]' )) ALTER TABLE [dbo].[StudentGrade] WITH CHECK ADD CONSTRAINT [FK_StudentGrade_Student] FOREIGN KEY ([StudentID]) REFERENCES [dbo].[Person] ([PersonID]) GO ALTER TABLE [dbo].[StudentGrade] CHECK CONSTRAINT [FK_StudentGrade_Student] GO -- Define the relationship between CourseInstructor and Course. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_CourseInstructor_Course]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[CourseInstructor]' )) ALTER TABLE [dbo].[CourseInstructor] WITH CHECK ADD CONSTRAINT [FK_CourseInstructor_Course] FOREIGN KEY ([CourseID]) REFERENCES [dbo].[Course] ([CourseID]) GO ALTER TABLE [dbo].[CourseInstructor] CHECK CONSTRAINT [FK_CourseInstructor_Course] GO -- Define the relationship between CourseInstructor and Person. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_CourseInstructor_Person]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[CourseInstructor]' )) ALTER TABLE [dbo].[CourseInstructor] WITH CHECK ADD CONSTRAINT [FK_CourseInstructor_Person] FOREIGN KEY ([PersonID]) REFERENCES [dbo].[Person] ([PersonID]) GO ALTER TABLE [dbo].[CourseInstructor] CHECK CONSTRAINT [FK_CourseInstructor_Person] GO -- Define the relationship between Course and Department. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_Course_Department]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[Course]' )) ALTER TABLE [dbo].[Course] WITH CHECK ADD CONSTRAINT [FK_Course_Department] FOREIGN KEY ([DepartmentID]) REFERENCES [dbo].[Department] ([DepartmentID]) GO ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_Department] GO --Define the relationship between OfficeAssignment and Person. IF NOT EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N '[dbo].[FK_OfficeAssignment_Person]' ) AND parent_object_id = OBJECT_ID(N '[dbo].[OfficeAssignment]' )) ALTER TABLE [dbo].[OfficeAssignment] WITH CHECK ADD CONSTRAINT [FK_OfficeAssignment_Person] FOREIGN KEY ([InstructorID]) REFERENCES [dbo].[Person] ([PersonID]) GO ALTER TABLE [dbo].[OfficeAssignment] CHECK CONSTRAINT [FK_OfficeAssignment_Person] GO -- Create InsertOfficeAssignment stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[InsertOfficeAssignment]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[InsertOfficeAssignment] @InstructorID int, @Location nvarchar(50) AS INSERT INTO dbo.OfficeAssignment (InstructorID, Location) VALUES (@InstructorID, @Location); IF @@ROWCOUNT > 0 BEGIN SELECT [Timestamp] FROM OfficeAssignment WHERE InstructorID=@InstructorID; END ' END GO --Create the UpdateOfficeAssignment stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[UpdateOfficeAssignment]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[UpdateOfficeAssignment] @InstructorID int, @Location nvarchar(50), @OrigTimestamp timestamp AS UPDATE OfficeAssignment SET Location=@Location WHERE InstructorID=@InstructorID AND [Timestamp]=@OrigTimestamp; IF @@ROWCOUNT > 0 BEGIN SELECT [Timestamp] FROM OfficeAssignment WHERE InstructorID=@InstructorID; END ' END GO -- Create the DeleteOfficeAssignment stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[DeleteOfficeAssignment]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[DeleteOfficeAssignment] @InstructorID int AS DELETE FROM OfficeAssignment WHERE InstructorID=@InstructorID; ' END GO -- Create the DeletePerson stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[DeletePerson]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[DeletePerson] @PersonID int AS DELETE FROM Person WHERE PersonID = @PersonID; ' END GO -- Create the UpdatePerson stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[UpdatePerson]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[UpdatePerson] @PersonID int, @LastName nvarchar(50), @FirstName nvarchar(50), @HireDate datetime, @EnrollmentDate datetime AS UPDATE Person SET LastName=@LastName, FirstName=@FirstName, HireDate=@HireDate, EnrollmentDate=@EnrollmentDate WHERE PersonID=@PersonID; ' END GO -- Create the InsertPerson stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[InsertPerson]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[InsertPerson] @LastName nvarchar(50), @FirstName nvarchar(50), @HireDate datetime, @EnrollmentDate datetime AS INSERT INTO dbo.Person (LastName, FirstName, HireDate, EnrollmentDate) VALUES (@LastName, @FirstName, @HireDate, @EnrollmentDate); SELECT SCOPE_IDENTITY() as NewPersonID; ' END GO -- Create GetStudentGrades stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[GetStudentGrades]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[GetStudentGrades] @StudentID int AS SELECT EnrollmentID, Grade, CourseID, StudentID FROM dbo.StudentGrade WHERE StudentID = @StudentID ' END GO -- Create GetDepartmentName stored procedure. IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N '[dbo].[GetDepartmentName]' ) AND type in (N 'P' , N 'PC' )) BEGIN EXEC dbo.sp_executesql @statement = N ' CREATE PROCEDURE [dbo].[GetDepartmentName] @ID int, @Name nvarchar(50) OUTPUT AS SELECT @Name = Name FROM Department WHERE DepartmentID = @ID ' END GO -- Insert data into the Person table. USE School GO SET IDENTITY_INSERT dbo.Person ON GO INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (1, 'Abercrombie' , 'Kim' , '1995-03-11' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (2, 'Barzdukas' , 'Gytis' , null , '2005-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (3, 'Justice' , 'Peggy' , null , '2001-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (4, 'Fakhouri' , 'Fadi' , '2002-08-06' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (5, 'Harui' , 'Roger' , '1998-07-01' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (6, 'Li' , 'Yan' , null , '2002-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (7, 'Norman' , 'Laura' , null , '2003-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (8, 'Olivotto' , 'Nino' , null , '2005-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (9, 'Tang' , 'Wayne' , null , '2005-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (10, 'Alonso' , 'Meredith' , null , '2002-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (11, 'Lopez' , 'Sophia' , null , '2004-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (12, 'Browning' , 'Meredith' , null , '2000-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (13, 'Anand' , 'Arturo' , null , '2003-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (14, 'Walker' , 'Alexandra' , null , '2000-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (15, 'Powell' , 'Carson' , null , '2004-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (16, 'Jai' , 'Damien' , null , '2001-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (17, 'Carlson' , 'Robyn' , null , '2005-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (18, 'Zheng' , 'Roger' , '2004-02-12' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (19, 'Bryant' , 'Carson' , null , '2001-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (20, 'Suarez' , 'Robyn' , null , '2004-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (21, 'Holt' , 'Roger' , null , '2004-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (22, 'Alexander' , 'Carson' , null , '2005-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (23, 'Morgan' , 'Isaiah' , null , '2001-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (24, 'Martin' , 'Randall' , null , '2005-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (25, 'Kapoor' , 'Candace' , '2001-01-15' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (26, 'Rogers' , 'Cody' , null , '2002-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (27, 'Serrano' , 'Stacy' , '1999-06-01' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (28, 'White' , 'Anthony' , null , '2001-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (29, 'Griffin' , 'Rachel' , null , '2004-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (30, 'Shan' , 'Alicia' , null , '2003-09-01' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (31, 'Stewart' , 'Jasmine' , '1997-10-12' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (32, 'Xu' , 'Kristen' , '2001-7-23' , null ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (33, 'Gao' , 'Erica' , null , '2003-01-30' ); INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate) VALUES (34, 'Van Houten' , 'Roger' , '2000-12-07' , null ); GO SET IDENTITY_INSERT dbo.Person OFF GO -- Insert data into the Department table. INSERT INTO dbo.Department (DepartmentID, [ Name ], Budget, StartDate, Administrator) VALUES (1, 'Engineering' , 350000.00, '2007-09-01' , 2); INSERT INTO dbo.Department (DepartmentID, [ Name ], Budget, StartDate, Administrator) VALUES (2, 'English' , 120000.00, '2007-09-01' , 6); INSERT INTO dbo.Department (DepartmentID, [ Name ], Budget, StartDate, Administrator) VALUES (4, 'Economics' , 200000.00, '2007-09-01' , 4); INSERT INTO dbo.Department (DepartmentID, [ Name ], Budget, StartDate, Administrator) VALUES (7, 'Mathematics' , 250000.00, '2007-09-01' , 3); GO -- Insert data into the Course table. INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (1050, 'Chemistry' , 4, 1); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (1061, 'Physics' , 4, 1); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (1045, 'Calculus' , 4, 7); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (2030, 'Poetry' , 2, 2); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (2021, 'Composition' , 3, 2); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (2042, 'Literature' , 4, 2); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (4022, 'Microeconomics' , 3, 4); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (4041, 'Macroeconomics' , 3, 4); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (4061, 'Quantitative' , 2, 4); INSERT INTO dbo.Course (CourseID, Title, Credits, DepartmentID) VALUES (3141, 'Trigonometry' , 4, 7); GO -- Insert data into the OnlineCourse table. INSERT INTO dbo.OnlineCourse (CourseID, URL) INSERT INTO dbo.OnlineCourse (CourseID, URL) INSERT INTO dbo.OnlineCourse (CourseID, URL) INSERT INTO dbo.OnlineCourse (CourseID, URL) --Insert data into OnsiteCourse table. INSERT INTO dbo.OnsiteCourse (CourseID, Location, Days, [ Time ]) VALUES (1050, '123 Smith' , 'MTWH' , '11:30' ); INSERT INTO dbo.OnsiteCourse (CourseID, Location, Days, [ Time ]) VALUES (1061, '234 Smith' , 'TWHF' , '13:15' ); INSERT INTO dbo.OnsiteCourse (CourseID, Location, Days, [ Time ]) VALUES (1045, '121 Smith' , 'MWHF' , '15:30' ); INSERT INTO dbo.OnsiteCourse (CourseID, Location, Days, [ Time ]) VALUES (4061, '22 Williams' , 'TH' , '11:15' ); INSERT INTO dbo.OnsiteCourse (CourseID, Location, Days, [ Time ]) VALUES (2042, '225 Adams' , 'MTWH' , '11:00' ); INSERT INTO dbo.OnsiteCourse (CourseID, Location, Days, [ Time ]) VALUES (4022, '23 Williams' , 'MWF' , '9:00' ); -- Insert data into the CourseInstructor table. INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (1050, 1); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (1061, 31); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (1045, 5); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (2030, 4); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (2021, 27); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (2042, 25); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (4022, 18); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (4041, 32); INSERT INTO dbo.CourseInstructor(CourseID, PersonID) VALUES (4061, 34); GO --Insert data into the OfficeAssignment table. INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (1, '17 Smith' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (4, '29 Adams' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (5, '37 Williams' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (18, '143 Smith' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (25, '57 Adams' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (27, '271 Williams' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (31, '131 Smith' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (32, '203 Williams' ); INSERT INTO dbo.OfficeAssignment(InstructorID, Location) VALUES (34, '213 Smith' ); -- Insert data into the StudentGrade table. INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2021, 2, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2030, 2, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2021, 3, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2030, 3, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2021, 6, 2.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2042, 6, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2021, 7, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2042, 7, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2021, 8, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (2042, 8, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4041, 9, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4041, 10, null ); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4041, 11, 2.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4041, 12, null ); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4061, 12, null ); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 14, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 13, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4061, 13, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4041, 14, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 15, 2.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 16, 2); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 17, null ); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 19, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4061, 20, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4061, 21, 2); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 22, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4041, 22, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4061, 22, 2.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (4022, 23, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1045, 23, 1.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1061, 24, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1061, 25, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1050, 26, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1061, 26, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1061, 27, 3); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1045, 28, 2.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1050, 28, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1061, 29, 4); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1050, 30, 3.5); INSERT INTO dbo.StudentGrade (CourseID, StudentID, Grade) VALUES (1061, 30, 4); GO |
3:RIA之ADO.NET ENTITY DATA MODEL
为web创建ADO.NET ENTITY DATA MODEL,如图:
注意,习惯命名规则:Model数据库名.edmx。
在下一步中,我们选择刚才创建的数据库:
选择新建连接,按照指示完成数据库连接配置。
进入下一步:
在这一步中,注意一定要选择第一个红框中的内容,否则默认不能生成联表查询。在下一步中,我们选择“Generate from database”,如下:
点击Finish后,VS为我们生成了一个以edmx为后缀的文件。OK,到此暂停,我们先来看看这个edmx文件是干什么用的,为我们完成了什么工作。
3.1:什么是EDM
ENTITY DATA MODEL,简写为EDM,中文为实体数据模型。它由三个概念组成。概念模型由概念架构定义语言文件 (.csdl)来定义,映射由映射规范语言文件 (.msl),存储模型(又称逻辑模型)由存储架构定义语言文件 (.ssdl)来定义。这三者合在一起就是EDM模型。EDM模型在项目中的表现形式就是扩展名为.edmx的文件。
Entity Framework实现了一套类似于ADO.NET2.0中的连接类来操作EDM完成持久化。EntityFramework中所有发往EDM的操作都是经过EntityClient,包括使用LINQ to Entity进行的操作。目前可用操作如下:
4:针对web EMD的测试
理解了EMD在开发中所处的作用,我们就可以针对EDM来写个测试项目。为了简便期间,我们直接在EMD中的SchoolEntities类型的构造方法中直接创建测试(严格意义来说,这不是个单元测试,这仅是测试)。
针对这个操作,会在测试项目中生成一个SchoolEntitiesTest的类型,同时,在这个类型中会生成一个SchoolEntitiesConstructorTest的方法,如下:
由于我们仅仅测试EDM,而跟WEB本身没有关系,所有我们注释掉了红框中的内容。同时我们写入真正的测试代码,如下:
这里的一个小细节是,连接字符串我们通过编码的方式传入到测试方法。EDM的连接字符串和ADO.NET的连接字符串有很大不同,在这里,可公开一下我们的生成EDM连接字符串的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public string GetConnectionString() { string providerName = "System.Data.SqlClient" ; string serverName = "192.168.0.96" ; string databaseName = "mysample" ; SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); sqlBuilder.DataSource = serverName; sqlBuilder.InitialCatalog = databaseName; sqlBuilder.IntegratedSecurity = false ; sqlBuilder.UserID = "sa" ; sqlBuilder.Password = "sasa" ; string providerString = sqlBuilder.ToString(); EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(); entityBuilder.Provider = providerName; entityBuilder.ProviderConnectionString = providerString; entityBuilder.Provider = providerName; entityBuilder.Metadata = "res://*/Modelmysample.csdl|res://*/Modelmysample.ssdl|res://*/Modelmysample.msl" ; return entityBuilder.ToString(); } |
调试我们的测试代码,最终我们会获取到数据库中的4条记录。
通过了以上的阐述,我们了解了EDM的作用,以及如何测试EDM。接下来,我们需要知道如何在SL APP中调用WEB中的EDM。
5:Domain Service Class
在SL APP中调用EDM是通过WEB的Domain Service Class来实现的。简单的说来,它是RIA SERVICE框架中的一个重要内容。
在WEB选择添加新项,选择Domain Service Class,命名,下一步会出现如下界面:
我们可以做出如上勾选。确定后,编译整个解决方法,会发现目前的解决方案结构会变成如下形式:
注意,第一个红框部分,是VS自动为我们在SL APP生成的。它是第二个红框在客户端的对应版本。我们需要将它包含到项目中去。它包含了所有服务端版本类中定义的方法、实体等,而可在客户端直接调用。
6:Ria Service数据流转
好了,经过以上的描述我们知道了:
EDM:直接操作数据库;
Domain Service Class:调用EDM,并把数据接口通过WCF的形式开放给客户端;
*.Web.g.cs:调用Domain Service Class,完成对数据的读取,并最终呈现给UI;
7:最终展示
在SL APP中的UI调用呈现数据。前台:
后台,直接使用*.Web.g.cs中的DomainServiceMySample 加载数据:
1 2 3 4 5 6 7 | public MainPage() { InitializeComponent(); DomainServiceMySample context = new DomainServiceMySample(); context.Load(context.GetEmpQuery()); lb1.ItemsSource = context.emps; } |
最后界面显示:
8:问题
该示例演示了使用Entity Framework和WCF Ria Services进行Silverlight开发。我们当然可以继续使用此模式完善功能,知道一个功能复杂的应用程序开发完毕。但是,当前,此示例起码存在如下几个问题。
1:实体模型被紧耦合在EDM中,同时它不能项目(模块)使用。随着每一次更新EDM,实体模型会被覆盖;
2:EDM和BLL紧耦合在一起;
3:没有提炼出数据接口,导致我们没有办法在此示例中进行单元测试。示例中虽然存在测试项目,但那是数据库相关的,达不到单元测试的要求;
下篇我们将继续重构该示例,以逐步解决这些问题。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器