今天记录的内容不多,只是简单用一下Model first,新建项目,然后添加新建项,选择数据中的ado.net实体数据模型
这次我们选择空模型,然后右键,新增,实体
在这项demo中我打算建两个数据实体,一个studentInfo,classInfo。
得到类似uml图
我们可以在每个图上新增属性,各新增了name属性,在studentInfo上新增cId,空白处右键,新增关联,
右键根据模型生成数据库
实现在数据库建好库,然后选择数据库,就会生成sql语句了。下面是我生成的sql语句
1 -- -------------------------------------------------- 2 -- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure 3 -- -------------------------------------------------- 4 -- Date Created: 06/15/2014 22:37:13 5 -- Generated from EDMX file: C:\Users\admin\documents\visual studio 2012\Projects\EFConsole\ConsoleApplication1\Model1.edmx 6 -- -------------------------------------------------- 7 8 SET QUOTED_IDENTIFIER OFF; 9 GO 10 USE [EFtest]; 11 GO 12 IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]'); 13 GO 14 15 -- -------------------------------------------------- 16 -- Dropping existing FOREIGN KEY constraints 17 -- -------------------------------------------------- 18 19 20 -- -------------------------------------------------- 21 -- Dropping existing tables 22 -- -------------------------------------------------- 23 24 25 -- -------------------------------------------------- 26 -- Creating all tables 27 -- -------------------------------------------------- 28 29 -- Creating table 'Student' 30 CREATE TABLE [dbo].[Student] ( 31 [id] int IDENTITY(1,1) NOT NULL, 32 [name] nvarchar(max) NOT NULL, 33 [cId] nvarchar(max) NOT NULL, 34 [ClassInfo_id] int NOT NULL 35 ); 36 GO 37 38 -- Creating table 'ClassInfo' 39 CREATE TABLE [dbo].[ClassInfo] ( 40 [id] int IDENTITY(1,1) NOT NULL, 41 [name] nvarchar(max) NOT NULL 42 ); 43 GO 44 45 -- -------------------------------------------------- 46 -- Creating all PRIMARY KEY constraints 47 -- -------------------------------------------------- 48 49 -- Creating primary key on [id] in table 'Student' 50 ALTER TABLE [dbo].[Student] 51 ADD CONSTRAINT [PK_Student] 52 PRIMARY KEY CLUSTERED ([id] ASC); 53 GO 54 55 -- Creating primary key on [id] in table 'ClassInfo' 56 ALTER TABLE [dbo].[ClassInfo] 57 ADD CONSTRAINT [PK_ClassInfo] 58 PRIMARY KEY CLUSTERED ([id] ASC); 59 GO 60 61 -- -------------------------------------------------- 62 -- Creating all FOREIGN KEY constraints 63 -- -------------------------------------------------- 64 65 -- Creating foreign key on [ClassInfo_id] in table 'Student' 66 ALTER TABLE [dbo].[Student] 67 ADD CONSTRAINT [FK_StudentInfoClassInfo] 68 FOREIGN KEY ([ClassInfo_id]) 69 REFERENCES [dbo].[ClassInfo] 70 ([id]) 71 ON DELETE NO ACTION ON UPDATE NO ACTION; 72 73 -- Creating non-clustered index for FOREIGN KEY 'FK_StudentInfoClassInfo' 74 CREATE INDEX [IX_FK_StudentInfoClassInfo] 75 ON [dbo].[Student] 76 ([ClassInfo_id]); 77 GO 78 79 -- -------------------------------------------------- 80 -- Script has ended 81 -- --------------------------------------------------
将代码复制到数据库中执行以下就可以了,这就是Model first。code first与Model first并不是一样的。code first 需要我们自己写类,用的不是很多。