您的位置:首页 > 其它

Entity Framework 学习第二天

2014-06-15 22:40 169 查看
今天记录的内容不多,只是简单用一下Model first,新建项目,然后添加新建项,选择数据中的ado.net实体数据模型



这次我们选择空模型,然后右键,新增,实体



在这项demo中我打算建两个数据实体,一个studentInfo,classInfo。





得到类似uml图



我们可以在每个图上新增属性,各新增了name属性,在studentInfo上新增cId,空白处右键,新增关联,



右键根据模型生成数据库



实现在数据库建好库,然后选择数据库,就会生成sql语句了。下面是我生成的sql语句

-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/15/2014 22:37:13
-- Generated from EDMX file: C:\Users\admin\documents\visual studio 2012\Projects\EFConsole\ConsoleApplication1\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [EFtest];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Student'
CREATE TABLE [dbo].[Student] (
[id] int IDENTITY(1,1) NOT NULL,
[name] nvarchar(max)  NOT NULL,
[cId] nvarchar(max)  NOT NULL,
[ClassInfo_id] int  NOT NULL
);
GO

-- Creating table 'ClassInfo'
CREATE TABLE [dbo].[ClassInfo] (
[id] int IDENTITY(1,1) NOT NULL,
[name] nvarchar(max)  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [id] in table 'Student'
ALTER TABLE [dbo].[Student]
ADD CONSTRAINT [PK_Student]
PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'ClassInfo'
ALTER TABLE [dbo].[ClassInfo]
ADD CONSTRAINT [PK_ClassInfo]
PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ClassInfo_id] in table 'Student'
ALTER TABLE [dbo].[Student]
ADD CONSTRAINT [FK_StudentInfoClassInfo]
FOREIGN KEY ([ClassInfo_id])
REFERENCES [dbo].[ClassInfo]
([id])
ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StudentInfoClassInfo'
CREATE INDEX [IX_FK_StudentInfoClassInfo]
ON [dbo].[Student]
([ClassInfo_id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------


将代码复制到数据库中执行以下就可以了,这就是Model first。code first与Model first并不是一样的。code first 需要我们自己写类,用的不是很多。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: