Enable Entity Framework to stop private access to sys. databases and sysdatabases

Source: Internet
Author: User

Enable Entity Framework to stop private access to sys. databases and sysdatabases

Here, "Private sys. databases" refers to the default query initiated by Entity Framework: SELECT Count (*) FROM sys. databases WHERE [name] = n' database name'

Note: This article is intended for the Entity Framework Code First scenario. The Entity Framework versions are 4.1 and 4.2.

In February March, two background transactions of Entity Framework were discovered in the first article about Entity Framework LINQ queries.

The first transaction (private competition sys. databases ):

SELECT Count (*) FROM sys. databases WHERE [name] = n' cnblogsdata'

The second transaction (unauthorized exploration of database information ):

Select top (1)
[Extent1]. [Id] AS [Id],
[Extent1]. [ModelHash] AS [ModelHash]
FROM [dbo]. [ed1_adata] AS [Extent1]
Order by [Extent1]. [Id] DESC

At that time, modelBuilder. Conventions. Remove <IncludeMetadataConvention> (); blocked the second transaction, but failed to find a way to block the first transaction.

I remember that I used Entity Framework 4.0. Later, Microsoft released EF4.1 and EF4.2. Let's see if Entity Framework has been corrected.

The result shows that only half of the transaction is changed. The second transaction is called "return", but the first transaction "one piece is not changed ".

The world of code is a perfect world. Such behaviors are never allowed and must be brought to justice.

After this afternoon's efforts, I finally found this "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)
{
}
}

It can be seen that the Entity Framework "Private sys. databases" aims to check whether a database with the same name exists when it is generated based on the Entity.

In our application scenarios, the Entity Framework does not need to generate a database, so it is "Justified ".

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.