码迷,mamicode.com
首页 > 数据库 > 详细

Entity Framework Code First to an Existing Database

时间:2017-07-08 11:19:26      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:post   enter   menu   data   form data   delete   ima   ever   arc   

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/)
View Code

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

  • 技术分享

     

Entity Framework Code First to an Existing Database

标签:post   enter   menu   data   form data   delete   ima   ever   arc   

原文地址:http://www.cnblogs.com/liandy0906/p/7136008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!