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)