AOD.NET Entity Framework 实体框架 学习(2 模型和映射)

MSDN中的《生成模型和映射章节》

实体框架应用程序和服务基于以三种基于 XML 的语言表示的模型和映射信息:

CSDL 将应用程序数据表示为概念模型中的一组实体和关系,它是 实体数据模型的实现。 以 SSDL 表示的存储模型表示数据存储的架构。上述两种模型之间的映射以 MSL 表示。

ADO.NET 实体数据模型工具可自动生成应用程序的 CSDL、SSDL 和 MSL 内容。 实体数据模型向导从现有数据库生成模型和映射信息以及数据类。 然后,可以使用 ADO.NET 实体数据模型设计器(实体设计器)以图形方式修改模型和映射信息。 或者,可以使用实体设计器以图形方式创建一个概念模型,接着使用 “生成数据库”向导(实体数据模型工具)自动生成支持此概念模型的存储模型、映射、数据类和数据库。 在任一方案中,当基础数据库发生更改时,您都可以使用 模型更新向导(实体数据模型工具)更新模型和映射信息。

EDM 生成器 (EdmGen.exe) 是一种命令行工具,通过此工具,您可以生成简单的概念模型,在该模型中实体与数据源中的表之间具有一对一的映射关系。 您还可以使用 EdmGen.exe 验证概念模型,并生成基于模型中的实体类型的数据类。EdmGen.exe 是 .NET Framework 版本 3.5 Service Pack 1 (SP1) 或更高版本中的实体框架运行时组件的一部分。

    这部分介绍了一下生成实体模型的时候所需要的三个文件CSDL、SSDL、MSL。其中也说明了有工具来自动生成这三文件的个内容。一个是在项目中通过《实体数据模型向导》来生成,并且可以通过《ADO.NET 实体数据模型设计器》来以图形方式来修改模型和映射信息。另一个工具是通过EDM生成器命令行工具来生成简单的概念模型。我们这里没必要使用这个命令行工具了,有强大的界面向导工具为什么不用呢?!

当您使用实体数据模型工具从现有数据库生成概念模型时,需要考虑以下注意事项:

  • 所有实体都必须具有键。如果数据库中有一个未设置主键的表,那么实体数据模型工具会尝试为相应的实体推断一个键。此外,实体数据模型工具会在存储架构中生成一个 DefiningQuery 元素,使此实体的数据为只读。 若要使此实体数据成为可更新数据,必须确认所生成的键是有效键,然后删除 DefiningQuery 元素。

  • 仅包含外键、表示数据库中两个表之间的多对多关系的表(有时称为纯联接表)在概念模型中没有对应的实体。当实体数据模型工具遇到此类表时,会在概念模型中将该表表示为一个多对多关联,而不是实体。这种行为的一个示例就是 School 模型中的 CourseInstructor 关联。

    看到上面红色字体的注意事项!一个是注意主键,一个是注意外键!

    要想创建模型,得有个数据库才行。这里使用MSDN上面的School示例数据库。数据库代码如下:

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



-- 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)

VALUES (2030, 'http://www.fineartschool.net/Poetry');

INSERT INTO dbo.OnlineCourse (CourseID, URL)

VALUES (2021, 'http://www.fineartschool.net/Composition');

INSERT INTO dbo.OnlineCourse (CourseID, URL)

VALUES (4041, 'http://www.fineartschool.net/Macroeconomics');

INSERT INTO dbo.OnlineCourse (CourseID, URL)

VALUES (3141, 'http://www.fineartschool.net/Trigonometry');



--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

数据库的架构关系图如下:

 

    建好数据库后,我们就可以开始在项目中创建该数据库的实体模型了。

    步骤如下:

1.打开VS2010,新建项目(项目类型随便你选择,我这里选择创建MVC3项目并使用Razor模板)

2.在项目的Models文件夹下添加新项,在选择新项模板对话框中选择“ADO.NET实体数据模型”,将该项名字命名为School。

3.上一步点确定之后就会打开“实体数据模型向导”,这里选择“从数据库生成”,点击下一步。

image

4.在“选择您的数据库连接”中新建连接。在弹出的连接属性里面输入你的数据库服务器和登陆密码,以及选择好你要连接的数据库。然后在“选择您的数据库连接”对话框中将复选框的钩打上。如图:

image

5.在“选择数据库对象”对话框中展开表,选中你要的表(存储过程之后介绍),然后将“确定所生成对象名称的单复数形式”打勾。如图:

image

6.点击完成之后,VS会自动打开该实体模型的设计器窗体。School数据库所对应的实体模型就构建好了,如图所示:

image

从这个图来看,非常像在SysBase PowerDesigner中绘制的ConceptualDataModel。描述了实体之间的关系(E-R)图。

    School实体模型创建好了之后生成了一个School.edmx文件,还有一个是School.Designer.cs文件。

    edmx文件直接双击打开会启用设计器打开的,这里我们想要查看其具体内容的话,可以使用XML编辑器来打开该文件。该文件中的内容分为两大部分:一部分是edmx:Runtime,实体的运行时;一部分是Designer用于设计器呈现界面效果用的。代码如下图所示:

image

在Runtime部分的代码有明确的标明分别是SSDL(存储架构定义语言)、CSDL(概念架构定义语言)、C-S mapping(映射规范使用映射规范语言 (MSL))。此部分的代码不细看了,如果有兴趣的可以查看以下MSDN的具体说明。

    Designer.cs打开之后分为两个部分代码:一部分是实体上下文;另一部分是实体类。

    SchoolEntities这个类就是实体上下文,该类继承自ObjectContext,该类提供一些用于查询和使用对象形式的实体数据的功能。

该类提供三个构造函数重载,可以带数据库的连接字符串,或者实体连接对象,或者什么都不填用默认的连接。

image

属性部分就是对应每个实体的ObjectSet集合对象。

image

此外该类还提供一系列的AddTo方法来讲某实体添加到实体模型中。但是在文档中看到一句注释,说该方法已经弃用!改用关联的ObjectSet的add方法。

image

    Designer.cs文件中的代码除了SchoolEntities实体对象外,还有一部分就是实体类。这些类都是继承自EntityObject类来的。

每个继承自EntityObject的类都有一个工厂方法来构建新的自身对象。

其实这个对象就是对应数据库中的一个表,并且基元属性对应这个表中的字段。

另外还有一些导航属性,导航属性就是描述数物理模型中各个表之间的关系的。将来可以通过这个导航属性来访问到相关联的数据。非常实用方便。

该部分的代码非常多,并且也不是很复杂,这里就不详细的列出来说明了。有兴趣的可以自己查看。

 

你可能感兴趣的:(framework)