The truth is: Why does Entity Framework not support cross-database queries (solution attached)

Source: Internet
Author: User
Tags reflector

In the previous article, we found that when the Entity Framework builds an SQL statement, the totable ("cnblogstex. DBO. "cnblogstex. DBO. blog_postbody "to" [cnblogstext. DBO]. [blog_postbody] ", resulting in the inability to perform cross-database queries.

This morning, we used reflectorCodeAnalysis to find out the truth.

The truth is as follows:

1. For the "cnblogstex. DBO. blog_postbody" string, Entity Framework splits it into schema name (cnblogstex. DBO) and database table name (blog_postbod ).

This part is processed in the parsequalifiedtablename () method of system. Data. entity. modelconfiguration. Utilities. objectextensions. The reflector code is as follows:

 Public     Static     Void  Parsequalifiedtablename (  String Qualifiedname,  Out     String  Schemaname,  Out     String  Tablename)
{
Qualifiedname = Qualifiedname. Trim ();
Int Length = Qualifiedname. lastindexof ( ' . ' );
Schemaname = Null ;
Tablename = Qualifiedname;
Switch (Length)
{
Case - 1 :
Break ;

Case 0 :
Throw Error. totable_invalidschemaname (qualifiedname );

Default :
If (Length = (Tablename. Length - 1 ))
{
Throw Error. totable_invalidtablename (qualifiedname );
}
Schemaname = Qualifiedname. substring ( 0 , Length );
Tablename = Qualifiedname. substring (Length + 1 );
Break ;
}
If ( String . Isnullorwhitespace (schemaname ))
{
Schemaname = Null ;
}
}

2. add square brackets (cnblogstex. DBO becomes [cnblogstex. DBO], blog_postbod to [blog_postbod]) is in the system. data. sqlclient. the reflector code is as follows:

 Private     Void  Appendidentifier ( String  Identifier)
{
This . Appendsql ( " [ " + Identifier. Replace ( " ] " , " ] " ) + " ] " );
}

Therefore, when we change the table name to "cnblogstext]. [DBO. blog_postbody "," cnblogstext]. [DBO "is converted to" [cnblogstext]. [DBO] ".

There are not only codes with truth, but also diagrams with truth:

Knowing the truth, I can only look at the truth to sigh. It is still unknown whether the problem can be solved...

Update:

Killkill's reply turned "sigh" into "excited ".ProgramExclusive to members, the excitement that ordinary people cannot enjoy...

Originally, it was not the Entity Framework to be spoofed, but the SQL server. Synonym can be used to easily solve this problem. The SQL statement for creating synonyms is as follows:

CreateSynonym[DBO].[Cnblogstext _ blog_postbody] For [Cnblogstext].[DBO].[Blog_postbody]

Thanks for the help of killkill!

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.