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!