Entity Framework Code First to an Existing Database

Source: Internet
Author: User

1. Create an Existing Database
CREATE TABLE [dbo].[Blogs] (     [BlogId] INT IDENTITY(1,1) not NULL,     [Name] NVARCHAR( $)NULL,     [URL]  NVARCHAR( $)NULL,     CONSTRAINT [pk_dbo. Blogs] PRIMARY KEY CLUSTERED([BlogId] ASC) ); CREATE TABLE [dbo].[Posts] (     [PostID] INT IDENTITY(1,1) not NULL,     [Title] NVARCHAR( $)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 Code2. 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
    • Project, File--New ...
    • 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 to the ADO 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

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.