首页 > 代码库 > Entity Framework Code First to an Existing Database
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
-
Entity Framework Code First to an Existing Database
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。