zoukankan      html  css  js  c++  java
  • EF基础知识小记四(数据库=>模型设计器)

    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基础知识小记三(设计器=>数据库)介绍的一样

  • 相关阅读:
    Bootstrap-模态框Modal使用
    MVC Controller return 格式
    数据库水平拆分和垂直拆分区别(以mysql为例)
    MySQL 对于大表(千万级),要怎么优化呢?
    mysql优化案例
    Mysql大表查询优化技巧总结及案例分析
    Mysql Partition 理论知识总结
    mysql Partition(分区)初探
    MySQL partition分区I
    xapian安装
  • 原文地址:https://www.cnblogs.com/GreenLeaves/p/7646800.html
Copyright © 2011-2022 走看看