EF基础知识小记三(设计器=>数据库)介绍了如何创建一个空设计器模型,并如何将模型同步到数据库的表中,本文则主要介绍如何将一个存在的数据库同步到模型设计器中。为了能快速的模拟这个过程,给出一下建表语句,代码如下:
--建表脚本 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 StudentTeacher add 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、看过EF基础知识小记三(设计器=>数据库)后,省去一些简单的操作步骤,直接到下面这步操作
根据数据库生成edmx
2、选择指定的数据库,并选择响应的表生成edmx模型设计器
3、点击确认,生成成功,如下图:
4、增删查该的操作和EF基础知识小记三(设计器=>数据库)介绍的一样