Entity Framework Code First to an Existing Database

1. Create an Existing Database

CREATE TABLE [dbo].[Blogs] (
    [BlogId] INT IDENTITY (1, 1) NOT NULL,
    [Name] NVARCHAR (200) NULL,
    [Url]  NVARCHAR (200) NULL,
    CONSTRAINT [PK_dbo.Blogs] PRIMARY KEY CLUSTERED ([BlogId] ASC)
); 

CREATE TABLE [dbo].[Posts] (
    [PostId] INT IDENTITY (1, 1) NOT NULL,
    [Title] NVARCHAR (200) NULL,
    [Content] NTEXT NULL,
    [BlogId] INT NOT NULL,
    CONSTRAINT [PK_dbo.Posts] PRIMARY KEY CLUSTERED ([PostId] ASC),
    CONSTRAINT [FK_dbo.Posts_dbo.Blogs_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [dbo].[Blogs] ([BlogId]) ON DELETE CASCADE
); 

INSERT INTO [dbo].[Blogs] ([Name],[Url])
VALUES (‘The Visual Studio Blog‘, ‘http://blogs.msdn.com/visualstudio/‘) 

INSERT INTO [dbo].[Blogs] ([Name],[Url])
VALUES (‘.NET Framework Blog‘, ‘http://blogs.msdn.com/dotnet/‘)

2. Create the Application

To keep things simple we’re going to build a basic console application that uses Code First to perform data access:

  • Open Visual Studio
  • File -> New -> Project…
  • Select Windows from the left menu and Console Application
  • Enter CodeFirstExistingDatabaseSample as the name
  • Select OK

3. Reverse Engineer Model

  • Project -> Add New Item…
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter BloggingContext as the name and click OK
  • This launches the Entity Data Model Wizard
  • Select Code First from Database and click Next
时间: 2024-10-22 23:56:57

Entity Framework Code First to an Existing Database的相关文章

Entity Framework Code First to a New Database

1. Create the Application To keep things simple we're going to build a basic console application that uses Code First to perform data access. Open Visual Studio File -> New -> Project- Select Windows from the left menu and Console Application Enter 

Create Primary Key using Entity Framework Code First

原文:http://www.codeproject.com/Articles/813912/Create-Primary-Key-using-Entity-Framework-Code-Fir Introduction This article describes the effect of Entity Framework Code First convention and configuration for creating Primary Key column. Entity Framew

Entity Framework Code First - Change Tracking

In this post we will be discussing about change tracking feature of Entity Framework Code First. Change tracking allows Entity framework to keep track of all the changes in entities' data. It might involve adding new entities to entities collection o

SQLITE WITH ENTITY FRAMEWORK CODE FIRST AND MIGRATION

Last month I’ve a chance to develop an app using Sqlite and Entity Framework Code First. Before I started with this project, I thought everything will be easy because Sqlite and Entity Framework are both popular framework. Maybe I just need to instal

Entity Framework Code First (三)Data Annotations

Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C

Entity Framework Code First数据库迁移(DB Migration)

一.手动迁移 第1步.启用数据库迁移 打开程序包管理器控制台 工具->库程序包管理器->程序包管理器控制台 打开控制台后,在控制台管理窗口输入 Enable-Migrations 指令,铵下回车键,到这里已启用了数据库迁移,但还没执行,结果如下图: 第2步.运行数据库迁移 在控制台管理窗口输入 Add-Migration指令,来新增一条数据库迁移版本,输入时必须要带上一个版本名称 Add-Migration AddProductCategoryTypeName,如下图: 运行完成后会在解决方案

Entity Framework Code First数据库连接

参考页面: http://www.yuanjiaocheng.net/entity/entitytypes.html http://www.yuanjiaocheng.net/entity/entity-relations.html http://www.yuanjiaocheng.net/entity/entity-lifecycle.html http://www.yuanjiaocheng.net/entity/code-first.html http://www.yuanjiaochen

Entity Framework Code First属性映射约定

参考页面: http://www.yuanjiaocheng.net/entity/code-first.html http://www.yuanjiaocheng.net/entity/mode-first.html http://www.yuanjiaocheng.net/entity/database-first.html http://www.yuanjiaocheng.net/entity/choose-development-approach.html http://www.yuan

Entity Framework Code First 常用方法集成

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86