介绍了如何创建一个空设计器模型,并如何将模型同步到数据库的表中,本文则主要介绍如何将一个存在的数据库同步到模型设计器中。为了能快速的模拟这个过程,给出一下建表语句,代码如下:
--建表脚本create table Student( Id int not null, Name varchar(30) not null, Age int not null)create table Teacher( Id int not null, Name varchar(30) not null, Age int not null)create table StudentTeacher( StudentId int not null, TeacherId int not null)create table InfoCard( Id int not null, [Money] int not null, StudentId int not null)--给表添加约束--单主键约束alter table Student add constraint [PK_People]primary key clustered (Id Asc)alter table InfoCard add constraint [PK_InfoCard]primary key clustered (Id Asc)alter table Teacher add constraint [PK_Teacher]primary key clustered (Id Asc)--双主键约束(多对多)alter table StudentTeacher add constraint [PK_StudentTeacher]primary key clustered (StudentId,TeacherId Asc)--双外键约束(多对多)alter table StudentTeacheradd constraint [FK_StudentTeacher_Student]foreign key (StudentId) references Student (Id) on delete no action on update no action --级联更新级联删除alter table StudentTeacher add constraint [FK_StudentTeacher_Teacher]foreign key (TeacherId) references Teacher (Id) on delete no action on update no action --但外键约束(一对多)alter table InfoCard add constraint [FK_InfoCard_Student]foreign key (StudentId) references Student (Id) on delete no action on update no action
1、看过后,省去一些简单的操作步骤,直接到下面这步操作
根据数据库生成edmx
2、选择指定的数据库,并选择响应的表生成edmx模型设计器
3、点击确认,生成成功,如下图:
4、增删查该的操作和介绍的一样