前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >EF基础知识小记四(数据库=>模型设计器)

EF基础知识小记四(数据库=>模型设计器)

作者头像
郑小超.
发布2018-01-26 17:09:01
5810
发布2018-01-26 17:09:01
举报
文章被收录于专栏:GreenLeavesGreenLeaves

EF基础知识小记三(设计器=>数据库)介绍了如何创建一个空设计器模型,并如何将模型同步到数据库的表中,本文则主要介绍如何将一个存在的数据库同步到模型设计器中。为了能快速的模拟这个过程,给出一下建表语句,代码如下:

代码语言:javascript
复制
--建表脚本
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基础知识小记三(设计器=>数据库)介绍的一样

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档