EF6 uses CodeFirst in the original database for general review (1. Set up the basic environment)

Source: Internet
Author: User

I thought it was done now. But I found many problems are still vague. You can only review the information and review it again.

Vs. net2013 ef6 mvc5 sqlserver2008

 

1. Create Database Blogging

2. Create a table and insert Test Data


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 /');

The tables in the database are as follows:

The preceding example shows the original database. CodeFirst is used in the current status.

Cascade Delete is used when the primary and Foreign keys are created. It will be changed to soft Delete later, so it will not be used.

3. create solutions and projects

4. Install the new EF version

5. Add Data Object

6. generated context and Object

Connection string generated in web. config

7. Add features for Blog object

7. Test the generated object

When you create a project, if you select none as the authentication method, the system does not import files such as verify js, You need to manually copy them into the project.

So far, the basic part of CodeFirst based on the existing database has been completed and passed the test.

 

This article references http://msdn.microsoft.com/zh-cn/data/jj200620

The copyright of this article and the original article belongs to the aforementioned author.

Related Article

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.