Let the Entity Framework no longer trespass sys.databases do not automatically create a database (reprint)

Source: Internet
Author: User

Original: http://www.cnblogs.com/dudu/archive/2011/12/27/entity_framework_sys_databases.html

The "Trespass sys.databases" here refers to a query initiated by the Entity Framework by default: SELECT Count (*) from sys.databases WHERE [name]=n ' database name '

Note: This article is for the Entity Framework Code first scenario, and the Entity Framework version is 4.1 and 4.2.

In March, two behind-the-scenes trading in the Entity Framework was discovered in the article revealing the Entity Framework LINQ query.

First trade (Trespass sys.databases):

SELECT Count (*) from sys.databases WHERE [name] = N ' Cnblogsdata '

Second transaction (information about the database privately):

SELECT TOP (1)
[Extent1]. [ID] as [ID],
[Extent1]. [Modelhash] As [Modelhash]
from [dbo]. [Edmmetadata] As [Extent1]
ORDER by [Extent1]. [Id] DESC

It was passed modelbuilder.conventions.remove<includemetadataconvention> (); Blocked the second transaction, but failed to find a way to block the first transaction.

I remember using the Entity Framework 4.0, and then Microsoft released EF4.1 and EF4.2. Let's take a look at whether the Entity Framework has straightened out.

The results found that only half changed, the second transaction "The Prodigal Son", and the first deal "zeixin not change".

The world of code is the perfect world, and it must never be allowed to be brought to justice.

After this afternoon's efforts, finally found the "rope":

Database.setinitializer<dbcontexttypename> (NULL);

The sample code is as follows:

public class Efunitofwork:dbcontext, iunitofwork
{
Public Efunitofwork ()
{
Database.setinitializer<efunitofwork> (NULL);
}

protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
}
}

As you can see, the objective of the entity Framework "Trespass sys.databases" is to check if a database with the same name exists when the database is generated from the entity.

Our scenario does not require the Entity Framework to generate the database at all, so it is "justified" to bring it to justice.

Let the Entity Framework no longer trespass sys.databases do not automatically create a database (reprint)

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.