Modernization of the blog garden-Entity Framework

Source: Internet
Author: User

In the development of the new version of the blog background of the blog garden, we have prepared the most advanced weapons to increase the pace of modernization of the garden. ASP. net mvc 3 has been installed before, and is currently experimenting with the latest new type of weapon, Entity Framework 4.1 RC, just introduced from Microsoft.

Test Analysis:

1. Regression of database connection strings

In earlier versions of Entity Framework, Microsoft has another set of connection strings, so that it cannot directly use the original ADO. net connection string. Therefore, we can only modify it to reuse the previous connection string. For details, refer to reusing the existing database connection string in Entity Framework.

In this version, Microsoft finally changed the connection string by using ADO. net,CodeAs follows:

Public ClassBlogdbcontext: dbcontext
{
PublicBlogdbcontext ()
:Base(Configurationmanager. connectionstrings ["Blogdb"]. Connectionstring)
{}
}

This is so good. Although it is a new weapon, the original shells are still used.

2. Faulty Entity Data Model

This bug does not know whether it is vs 2010 SP1 or Entity Framework 4.1 RC. You cannot create Entity Data model through the Entity Data Model Wizard in vs2010. See:

Click Next and select the connection string window to flash through... and nothing happens, and the. EDM file is not created either.

Is vs2010 playing a hide-and-seek game? Let's catch it... and finally find it. The method is to delete all database connections in Server Explorer of vs2010.

However, after the connection string is created, the "miracle" appears:

After all, it is not the official version, and some bugs are also excited, and we didn't expect Entity Data Model. We just wanted to generate entity classes from the database, saving effort. Since it fails, you can write the entity code.

3. Automatic ing between object classes and database tables

1) The "primary key" attribute is automatically found in the object class, provided that the attribute name is ID or object class name + id, such as the following object class:

 Public     Class  Blog
{
Public Int Blogid { Get ; Set ;}
Public String Application { Get ; Set ;}
Public String Author { Get ; Set ;}
Public String Username { Get ; Set ;}
}

Entity Framework automatically finds that blogid is the primary key. Of course, you can also specify the primary key through [Key.

2) If the attribute name of the object class is the same as the field name of the database table, the Entity Framework will automatically map the object class without any configuration.

3) if the class name of the object class is the same as that of the database table, the Entity Framework automatically maps the class name without any configuration. Otherwise, you must use the following code for manual ing:

Public ClassBlogdbcontext: dbcontext
{
Protected Override VoidOnmodelcreating (dbmodelbuilder modelbuilder)
{
Modelbuilder. Entity<Blog>(). Totable ("Blog_config");
}
}

Note: The blog is the entity class name, And the blog_config is the database table name.

If the conditions for automatic ing are met, it is very convenient to use Entity Framework. You only need to add an Entity Framework reference in the data access layer and add a class inherited from dbcontext, and then you can use it anytime, anywhere.

Conclusion:

Despite component failure, this weapon is easy to carry, flexible to deploy, and highly automated. It is essential for modernization!

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.