现在我要来为上面一节末尾给出的数据库(SchoolDB)创建实体数据模型;
SchoolDB数据库的脚本我已经写好了,如下:
USE master GO IF EXISTS(SELECT * FROM sys.sysdatabases WHERE name='SchoolDB') DROP DATABASE SchoolDB; GO CREATE DATABASE SchoolDB GO USE SchoolDB; GO --创建Standard表 IF EXISTS (SELECT * FROM sysobjects WHERE name='Standard') DROP TABLE [Standard]; GO CREATE TABLE [Standard] ( StandardID INT PRIMARY KEY , StandardName NVARCHAR(50), [Description] NVARCHAR(250) ); GO --创建Student表 IF EXISTS (SELECT * FROM sysobjects WHERE name='Student') DROP TABLE Student; GO CREATE TABLE Student ( StudentID INT PRIMARY KEY, StudentName NVARCHAR(100) NOT NULL, StandardID INT REFERENCES [Standard](StandardID), [RowVersion] NVARCHAR(50) ); GO --创建StudentAddress表 IF EXISTS (SELECT * FROM sysobjects WHERE name='StudentAddress') DROP TABLE StudentAddress; GO CREATE TABLE StudentAddress ( StudentID INT PRIMARY KEY, Address1 NVARCHAR(100) , Address2 NVARCHAR(100), City NVARCHAR(100), [State] NVARCHAR(100), CONSTRAINT StudentID_FK FOREIGN KEY(StudentID) REFERENCES dbo.Student(StudentID) ); GO --创建Teacher表 IF EXISTS (SELECT * FROM sysobjects WHERE name='Teacher') DROP TABLE Teacher; GO CREATE TABLE Teacher ( TeacherID INT PRIMARY KEY , TeacherName NVARCHAR(50), StandardID INT REFERENCES [Standard](StandardID), TeacherType NVARCHAR (100) ); GO --创建Course表 IF EXISTS (SELECT * FROM sysobjects WHERE name='Course') DROP TABLE Course; GO CREATE TABLE Course ( CourseID INT PRIMARY KEY , CourseName NVARCHAR(50), Location NVARCHAR(500), TeacherID INT REFERENCES dbo.Teacher(TeacherID) ); GO --创建StudentCourse表 IF EXISTS (SELECT * FROM sysobjects WHERE name='StudentCourse') DROP TABLE StudentCourse; GO CREATE TABLE StudentCourse ( StudentID INT REFERENCES Student(StudentID), CourseID INT REFERENCES dbo.Course(CourseID), CONSTRAINT StudentID_CourseID PRIMARY KEY(StudentID,CourseID), ); GO
首先我们打开上面一节创建的项目,选中“项目名称”,右键选择”属性“,我们要确保使用的.NET framework版本是4.5;
接下来,就是创建实体数据模型了:选中项目名称:
Note:
Pluralize or singularize generated object names checkbox singularizes an entityset name, if the table name in the database is plural. For example, if SchoolDB has Students table name then entityset would be singular Student. Similarly, relationships between the models will be pluralized if the table has one-to-many or many-to-many relationship with other tables. For example, Student has many-to-many relationship with Course table so Student entity set will have plural property name 'Courses' for the collection of courses.
The second checkbox, Include foreign key columns in the model, includes foreign key property explicitly to represent the foreign key. For example, Student table has one-to-many relationship with Standard table. So every student is associated with only one standard. To represent this in the model, Student entityset includes StandardId property with Standard navigation property. If this checkbox is unchecked then it will only include the Standard property, but not the StandardId in the Student entityset.
The third checkbox, Import selected stored procedures and functions into entity model, automatically creates Function Imports for the stored procedures and functions. You don't need to manually import this, as was necessary prior to Entity Framework 5.0.
7. After clicking on 'Finish', a School.edmx file will be added into your project.
Open EDM designer by double clicking on School.edmx. This displays all the entities for selected tables and the relationships between them as shown below:
现在我们以XML编译器的方式打开EDMX文件看看:
这篇没什么用,纯属扯淡的。可以略过。。。