EF6 in the original database use Codefirst Total review (first, build the basic environment)

Source: Internet
Author: User

Originally thought that already will, but when hands-on found many problems or vague, is so-called above his business. can only re-check the information, and then review again.

vs.net2013 Ef6 mvc5 sqlserver2008

I. Establishment of a database Blogging

Second, create the table, insert test data


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

The tables in the database are as follows:

The above is an example of an existing database that will be used in the current state Codefirst

The primary foreign key is created using cascade Delete, which is later changed to soft Delete, so it will not be used.

Iii. Creating solutions and projects

Iv. installation of the new EF

V. Adding a data Entity object

Vi. generated context and entity objects

Connection string generated in Web. config

Vii. adding attributes to a blog entity object

Vii. test-generated entity objects

When the project is established, if the authentication method is not selected, the system does not import files such as validation js, and it needs to be copied into the project manually.

At this point, the base part of the Codefirst based on the existing database has been completed and tested.

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

This article and the original copyright all belong to the above author all

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.