zoukankan      html  css  js  c++  java
  • Entity Framework 6.x

    Creating a Model from an Existing Database

    一、创建数据库 Chapter2

    USE master
    GO
    
    CREATE DATABASE Chapter2
    GO
    USE Chapter2
    GO
    
    CREATE TABLE [Meter]
    (
    [MeterId] [int] NOT NULL IDENTITY(1, 1),
    [MeterName] [varchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL
    ) ON [PRIMARY]
    GO
    ALTER TABLE [Meter] ADD CONSTRAINT [PK_Meter] PRIMARY KEY CLUSTERED  ([MeterId]) ON [PRIMARY]
    GO
    
    CREATE TABLE [Poet]
    (
    [PoetId] [int] NOT NULL IDENTITY(1, 1),
    [FirstName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
    [MiddleName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
    [LastName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL
    ) ON [PRIMARY]
    GO
    ALTER TABLE [Poet] ADD CONSTRAINT [PK_Poet] PRIMARY KEY CLUSTERED  ([PoetId]) ON [PRIMARY]
    GO
    
    CREATE TABLE [Poem]
    (
    [PoemId] [int] NOT NULL IDENTITY(1, 1),
    [PoetId] [int] NOT NULL,
    [Title] [varchar] (255) COLLATE Chinese_PRC_CI_AS NOT NULL,
    [MeterId] [int] NOT NULL
    ) ON [PRIMARY]
    GO
    ALTER TABLE [Poem] ADD CONSTRAINT [PK_Poem] PRIMARY KEY CLUSTERED  ([PoemId]) ON [PRIMARY]
    GO
    ALTER TABLE [Poem] ADD CONSTRAINT [FK_Poem_Meter] FOREIGN KEY ([MeterId]) REFERENCES [Meter] ([MeterId])
    GO
    ALTER TABLE [Poem] ADD CONSTRAINT [FK_Poem_Poet] FOREIGN KEY ([PoetId]) REFERENCES [Poet] ([PoetId])
    GO
    View Code

    二、创建项目 CreateModelFromExistingDatabase

    三、更新 Entity Framework 到最新版本

    四、添加模型文件

    完成后,得到如下图模型视图:

    重新编译一下项目。

    下面我们利用上面建立的模型对数据库进行操作,代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace CreateModelFromExistingDatabase
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //添加数据
    14             using (var context = new Chapter2Entities())
    15             {
    16                 var poet = new Poet { FirstName = "John", LastName = "Milton" };
    17                 var poem = new Poem { Title = "Paradise Lost" };
    18                 var meter = new Meter { MeterName = "Iambic Pentameter" };
    19                 poem.Meter = meter;
    20                 poem.Poet = poet;
    21                 context.Poems.Add(poem);
    22                 poem = new Poem { Title = "Paradise Regained" };
    23                 poem.Meter = meter;
    24                 poem.Poet = poet;
    25                 context.Poems.Add(poem);
    26 
    27                 poet = new Poet { FirstName = "Lewis", LastName = "Carroll" };
    28                 poem = new Poem { Title = "The Hunting of the Shark" };
    29                 meter = new Meter { MeterName = "Anapestic Tetrameter" };
    30                 poem.Meter = meter;
    31                 poem.Poet = poet;
    32                 context.Poems.Add(poem);
    33 
    34                 poet = new Poet { FirstName = "Lord", LastName = "Byron" };
    35                 poem = new Poem { Title = "Don Juan" };
    36                 poem.Meter = meter;
    37                 poem.Poet = poet;
    38                 context.Poems.Add(poem);
    39 
    40                 context.SaveChanges();
    41             }
    42             //显示数据
    43             using (var context = new Chapter2Entities())
    44             {
    45                 var poets = context.Poets;
    46                 foreach (var poet in poets)
    47                 {
    48                     Console.WriteLine("{0} {1}", poet.FirstName, poet.LastName);
    49                     foreach (var poem in poet.Poems)
    50                     {
    51                         Console.WriteLine("	{0} ({1})", poem.Title, poem.Meter.MeterName);
    52                     }
    53                 }
    54             }
    55             Console.ReadKey();
    56         }
    57     }
    58 }

    显示结果:

     在到数据库里看一下3个表情况。

  • 相关阅读:
    JS框架设计读书笔记之-选择器引擎02
    JS框架设计读书笔记之-选择器引擎01
    JS框架设计读书笔记之-小知识
    JS框架设计读书笔记之-函数
    JS框架设计读书笔记之-核心模块
    7.19 NOIP模拟6
    一 网络基础之网络协议篇
    Socket 网络编程
    Python 常用模块
    类的特殊成员
  • 原文地址:https://www.cnblogs.com/wanghaibin/p/3901756.html
Copyright © 2011-2022 走看看