Entity Framework learning Day 2

Source: Internet
Author: User

Today, there are not many records. Just use Model first to create a project, add a new item, and select the ado.net Object Data Model in the data.

This time we select an empty model, right-click it, add, Object

In this demo, I plan to create two data entities: studentInfo and classInfo.

Obtain a uml-like diagram

We can add properties on each graph, add the name attribute, add cId on studentInfo, right-click in the blank space, and add Association,

Right-click to generate Database Based on Model

After you create a database in the database and select a database, an SQL statement is generated. The following are the SQL statements I have generated:

 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 GO10 USE [EFtest];11 GO12 IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');13 GO14 15 -- --------------------------------------------------16 -- Dropping existing FOREIGN KEY constraints17 -- --------------------------------------------------18 19 20 -- --------------------------------------------------21 -- Dropping existing tables22 -- --------------------------------------------------23 24 25 -- --------------------------------------------------26 -- Creating all tables27 -- --------------------------------------------------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 NULL35 );36 GO37 38 -- Creating table 'ClassInfo'39 CREATE TABLE [dbo].[ClassInfo] (40     [id] int IDENTITY(1,1) NOT NULL,41     [name] nvarchar(max)  NOT NULL42 );43 GO44 45 -- --------------------------------------------------46 -- Creating all PRIMARY KEY constraints47 -- --------------------------------------------------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 GO54 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 GO60 61 -- --------------------------------------------------62 -- Creating all FOREIGN KEY constraints63 -- --------------------------------------------------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 GO78 79 -- --------------------------------------------------80 -- Script has ended81 -- --------------------------------------------------

Copy the code to the database and run the following command. This is Model first. Code first and Model first are not the same. Code first needs to write classes by ourselves, not many of which are used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.