文章出處

現在我要來為上面一節末尾給出的數據庫(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文件看看:

 

這篇沒什么用,純屬扯淡的。可以略過。。。


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()