前言 -- 数据库准备、一些名词准备

前言

本系列整理自EF 实体框架入门

数据库准备

该系列用到的数据库为 school,使用SQL Server。如果没有安装,可以使用VS附带的SQL Express。步骤省略,不清楚的可以使用“靠谱的”搜索引擎查找。

SQL脚本:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- 创建 Department 表。
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
-- 创建 Person 表。
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,
[Discriminator] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_School.Student] PRIMARY KEY CLUSTERED
(
[PersonID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
-- 创建 OnsiteCourse 表。
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
-- 创建 OnlineCourse 表。
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
-- 创建 StudentGrade 表。
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
-- 创建 CourseInstructor 表。
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
-- 创建 Course 表。
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
-- 创建 OfficeAssignment 表。
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
-- 定义 OnsiteCourse 与 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
-- 定义 OnlineCourse 与 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
-- 定义 StudentGrade 与 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
-- 定义 StudentGrade 与 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
-- 定义 CourseInstructor 与 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
-- 定义 CourseInstructor 与 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
-- 定义 Course 与 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
-- 定义 OfficeAssignment 与 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
-- 创建 InsertOfficeAssignment 存储过程。
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 [email protected];
END
‘
END
GO
-- 创建 UpdateOfficeAssignment 存储过程。
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 [email protected]
WHERE [email protected] AND [Timestamp][email protected];
IF @@ROWCOUNT > 0
BEGIN
SELECT [Timestamp] FROM OfficeAssignment
WHERE [email protected];
END
‘
END
GO
-- 创建 DeleteOfficeAssignment 存储过程。
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 [email protected];
‘
END
GO
-- 创建 DeletePerson 存储过程。
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
-- 创建 UpdatePerson 存储过程。
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,
@Discriminator nvarchar(50)
AS
UPDATE Person SET [email protected],
[email protected],
[email protected],
[email protected],
[email protected]
WHERE [email protected];
‘
END
GO
-- 创建 InsertPerson 存储过程。
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,
@Discriminator nvarchar(50)
AS
INSERT INTO dbo.Person (LastName,
FirstName,
HireDate,
EnrollmentDate,
Discriminator)
VALUES (@LastName,
@FirstName,
@HireDate,
@EnrollmentDate,
@Discriminator);
SELECT SCOPE_IDENTITY() as NewPersonID;
‘
END
GO
-- 创建 GetStudentGrades 存储过程。
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
-- 创建 GetDepartmentName 存储过程。
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
-- 将数据插到 Person 表中。
USE School
GO
SET IDENTITY_INSERT dbo.Person ON
GO
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (1, ‘Abercrombie‘, ‘Kim‘, ‘1995-03-11‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (2, ‘Barzdukas‘, ‘Gytis‘, null, ‘2005-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (3, ‘Justice‘, ‘Peggy‘, null, ‘2001-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (4, ‘Fakhouri‘, ‘Fadi‘, ‘2002-08-06‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (5, ‘Harui‘, ‘Roger‘, ‘1998-07-01‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (6, ‘Li‘, ‘Yan‘, null, ‘2002-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (7, ‘Norman‘, ‘Laura‘, null, ‘2003-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (8, ‘Olivotto‘, ‘Nino‘, null, ‘2005-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (9, ‘Tang‘, ‘Wayne‘, null, ‘2005-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (10, ‘Alonso‘, ‘Meredith‘, null, ‘2002-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (11, ‘Lopez‘, ‘Sophia‘, null, ‘2004-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (12, ‘Browning‘, ‘Meredith‘, null, ‘2000-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (13, ‘Anand‘, ‘Arturo‘, null, ‘2003-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (14, ‘Walker‘, ‘Alexandra‘, null, ‘2000-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (15, ‘Powell‘, ‘Carson‘, null, ‘2004-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (16, ‘Jai‘, ‘Damien‘, null, ‘2001-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (17, ‘Carlson‘, ‘Robyn‘, null, ‘2005-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (18, ‘Zheng‘, ‘Roger‘, ‘2004-02-12‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (19, ‘Bryant‘, ‘Carson‘, null, ‘2001-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (20, ‘Suarez‘, ‘Robyn‘, null, ‘2004-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (21, ‘Holt‘, ‘Roger‘, null, ‘2004-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (22, ‘Alexander‘, ‘Carson‘, null, ‘2005-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (23, ‘Morgan‘, ‘Isaiah‘, null, ‘2001-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (24, ‘Martin‘, ‘Randall‘, null, ‘2005-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (25, ‘Kapoor‘, ‘Candace‘, ‘2001-01-15‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (26, ‘Rogers‘, ‘Cody‘, null, ‘2002-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (27, ‘Serrano‘, ‘Stacy‘, ‘1999-06-01‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (28, ‘White‘, ‘Anthony‘, null, ‘2001-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (29, ‘Griffin‘, ‘Rachel‘, null, ‘2004-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (30, ‘Shan‘, ‘Alicia‘, null, ‘2003-09-01‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (31, ‘Stewart‘, ‘Jasmine‘, ‘1997-10-12‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (32, ‘Xu‘, ‘Kristen‘, ‘2001-7-23‘, null, ‘Instructor‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (33, ‘Gao‘, ‘Erica‘, null, ‘2003-01-30‘, ‘Student‘);
INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate, Discriminator)
VALUES (34, ‘Van Houten‘, ‘Roger‘, ‘2000-12-07‘, null, ‘Instructor‘);
GO
SET IDENTITY_INSERT dbo.Person OFF
GO
-- 将数据插到 Department 表中。
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
 
-- 将数据插到 Course 表中。
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
-- 将数据插到 OnlineCourse 表中。
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‘);
-- 将数据插到 OnsiteCourse 表中。
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‘);
-- 将数据插到 CourseInstructor 表中。
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
-- 将数据插到 OfficeAssignment 表中。
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‘);
-- 将数据插到 StudentGrade 表中。
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

参照 https://msdn.microsoft.com/zh-cn/data/jj614587

实体框架词汇表


术语


定义


Code First


使用代码创建实体框架模型。模型可以面向任何现有数据库或新数据库。


上下文


表示与数据库会话的类,用于查询和保存数据。上下文派生自 DbContext 或 ObjectContext 类。


约定 (Code First)


实体框架用来根据类推断模型形状的规则。


Database First


使用 EF 设计器创建面向现有数据库的实体框架模型。


预先加载


一种加载相关数据的模式,在这种模式下,对一种实体的查询会在查询过程中也加载相关实体。


EF 设计器


Visual Studio 中的可视化设计器,可用来使用方框和线创建实体框架模型。


Entity


表示客户、产品和订单等应用程序数据的类或对象。


实体数据模型


描述实体以及实体间关系的模型。


显式加载


一种加载相关数据的模式,在此模式下,将通过调用 API 来调用相关对象。


Fluent API


可用于配置 Code First 模型的 API。


外键关联


实体间的一种关联,其中,表示外键的属性包括在依赖实体的类中(即,Product 包含一个 CategoryId 属性)。


识别关系


一种关系,其中主体实体的主键是依赖实体的主键的一部分。在这种关系中,没有主体实体,依赖实体就不能存在。


独立关联


实体间的一种关联,其中,依赖实体的类中没有表示外键的属性(即,Product 类包含与 Category 的关系,但没有 CategoryId 属性)。实体框架将使用独立对象来跟踪这种关系。


延迟加载


一种加载相关数据的模式,在此模式下,访问导航属性时将自动加载相关对象。


Model First


使用 EF 设计器创建实体框架模型,随后使用此模型来创建新数据库。


导航属性


引用其他实体的实体属性(即,Product 包含 Category 导航属性,而 Category 包含 Products 导航属性)。


关系逆反


关系的相对一端,例如,product.Category 和 category.Product。


自跟踪实体


从代码生成模板生成的实体,这种实体有助于 N 层开发。


每个具体类型一张表 (TPC)


一种映射继承的方法,其中,层次结构中的每个非抽象类型都映射到数据库中单独的表。


每个层析结构一个表 (TPH)


一种映射继承的方法,其中,层次结构中的所有类型都映射到数据库中的同一个表。鉴别器列用于标识每行所关联的类型。


每个类型一张表 (TPT)


一种映射继承的方法,其中,层次结构中所有类型的公共属性都映射到数据库中的同一个表,但每个类型的唯一属性都映射到单独的表。


类型发现


对应该是实体框架模型的一部分的类型进行标识的过程。

时间: 2024-10-08 08:03:03

前言 -- 数据库准备、一些名词准备的相关文章

数据库分库分表

1. 数据库分库分表 1.1. 前言 1.1.1. 名词解释 1.2. 数据库架构演变 1.3. 分库分表前的问题 1.3.1. 用户请求量太大 1.3.2. 单库太大 1.3.3. 单表太大 1.4. 分库分表的方式方法 1.4.1. 垂直拆分 1.4.2. 水平拆分 1.5. 分库分表后面临的问题 1.5.1. 事务支持 1.5.2. 多库结果集合并(group by,order by) 1.5.3. 跨库join 1.6. 分库分表方案产品 1.7. 为什么不建议分库分表 1.8. 参考

【转】数据库锁机制

1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一个线程,T2 为另一个线程.T3,T4以此类推.下面以SQL Server(2005)为例. 2 锁的种类 共享锁(Shared lock). 例1: ---------------------------------------- T1: select * from table (请想象它需要执行

数据库锁机制

1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一个线程,T2 为另一个线程.T3,T4以此类推.下面以SQL Server(2005)为例. 2 锁的种类 共享锁(Shared lock). 例1: ---------------------------------------- T1: select * from table (请想象它需要执行

【转载】数据库大并发操作要考虑死锁和锁的性能问题

本文转载自:http://blog.csdn.net/yuanyuanispeak/article/details/52756167 1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一个线程,T2 为另一个线程.T3,T4以此类推.下面以SQL Server(2005)为例. 2 锁的种类 共享锁(Shared lock). 例1: -------

数据库的设计与维护

  前言 数据库是软件数据的核心部分,也可以说是软件的心脏部分,好的数据库设计会让软件具有较高的扩展性,以及很好的性能,差的数据库设计会大大缩短软件的生命周期,甚至直接导致软件上线不久就死亡.下面我就以我现在的经验,说一说,如何去设计一个好的数据库.   表命名和字段命名 表命名和字段命名要注意地方是: 一.命名不要使用中文,因为中文的编码是GB2312或者GBK,但现在主流的数据库,如MySql,Oracle,SqlServer默认编码都是UTF-8,如果为了途方便而使用中文,就避免不了要解决

数据库基础概念

1.数据库基本概念名词 数据库(Database, DB) 数据库管理系统(Database Management System, DBMS) 数据库管理员( Database Administrator, DBA ) 数据库系统( Database System, DBS ) 2.关系型数据库 关系型数据库(Relationship Database, RDB) 关系型数据库管理系统(RDBMS) SQL语言(Structured Query Language) 使用关系模型的数据库语言,用于

谈数据库的性能优化

这篇文章是我花了很多时间写出来的,曾经发表在javaeye论坛上,今天居然不见了,幸好网上有人转载这篇文章,没办法,只好再一份在博客里.这个是我以前写给我部门的一个技术心得,鄙人才疏学浅,知道javaeye高人很多,如果我写的不对的地方,欢迎指教. 我靠这么多关键字过滤啊,"fapiao"也成了关键字 1:前言 数据库优化是一个很广的范围,涉及到的东西比较多,并且每个特定的数据库,其具体的优化过程也是不一样的.因为优化的很大一部分最终都要跟具体的数据库系 统细节打交道,在此不可能针对所

MySQL/MariaDB数据库备份与恢复

前言 数据库一般存放着企业最为重要的数据,它关系到企业业务能否正常运转,数据库服务器总会遇到一些不可抗拒因素,导致数据丢失或损坏,而数据库备份可以帮助我们避免由于各种原因造成的数据丢失或着数据库的其他问题.本文将讲解MySQL/MariaDB数据库的几种备份方法. 基础知识 备份类型 完全备份:备份整个数据库 部分备份:仅备份其中的一张表或多张表 增量备份:仅备份从上次完全备份或增量备份之后变化的数据部分 差异备份:备份上次备份后变化的数据部分,和增量备份区别在于差异备份只可以相对完全备份做备份

mysql进阶(二十五)解决数据库NO CONNECTION问题

解决数据库NO CONNECTION问题 前言 数据库版本类型:Mysql5.5 在应用程序连接数据库时,提示数据库连接失败.打开数据库查看,显示如下. 究其原因,是因为mysql服务出现了问题,重启mysql服务,数据库成功连接. 参考文献 1.http://jingyan.baidu.com/album/0a52e3f4158211bf62ed72e6.html?picindex=1 美文美图