REST サービスの SQL 生成で大きな問題が発生しました。
次のコードは、2 つの異なる SQL ステートメントを生成しています (PC Win8 と Server 2008 R2)。
private void GetData()
{
ctPostBox = new CTPostBox(GetDBConnection(DatabaseKind.dkGlobal));
var lQry = (from d in ctPostBox.CONTACTUSER.Include("CONTACT").Include("CONTACT.DBLINK").Include("CONTACT.CONTACTLASTUSED").Include("CONTACT.EMAILACCOUNT")
where ((d.CONTACT.ID_DBLINK == 0 || d.CONTACT.ID_DBLINK == null) || (d.CONTACT.DBLINK.MANDNR == UserConfig.mandNr))
select new
{
READDATE = d.READDATE,
ID = d.CONTACT.ID,
DBID = d.CONTACT.DBID,
DATEINSERT = d.CONTACT.DATEINSERT,
FROMUSER = d.CONTACT.FROMUSER,
FILENAME = d.CONTACT.FILENAME,
KIND = d.CONTACT.KIND,
MSGID = d.CONTACT.MSGID,
ID_DBLINK = d.CONTACT.ID_DBLINK,
DBID_DBLINK = d.CONTACT.DBID_DBLINK,
SUBJECT = d.CONTACT.SUBJECT,
STATE = d.CONTACT.STATE,
DATEINCOME = d.CONTACT.DATEINCOME,
DATESEND = d.CONTACT.DATESEND,
ID_EMAILACCOUNT = d.CONTACT.ID_EMAILACCOUNT,
DBID_EMAILACCOUNT = d.CONTACT.DBID_EMAILACCOUNT,
GESCHST = d.CONTACT.DBLINK.GESCHST,
d.CONTACT.DBLINK.KEYTYP,
KONTO = d.CONTACT.EMAILACCOUNT.NAME
});
WriteFile(@"C:\Temp\log.log",lQry.ToString());
}
SQL 開発 PC (すべて問題ありません):
SELECT
"C"."ID_CONTACT" AS "ID_CONTACT",
"C"."READDATE" AS "READDATE",
"C"."DBID_CONTACT" AS "DBID_CONTACT",
"D"."DATEINSERT" AS "DATEINSERT",
"D"."FROMUSER" AS "FROMUSER",
"D"."FILENAME" AS "FILENAME",
"D"."KIND" AS "KIND",
"D"."MSGID" AS "MSGID",
"D"."ID_DBLINK" AS "ID_DBLINK",
"D"."DBID_DBLINK" AS "DBID_DBLINK",
"D"."SUBJECT" AS "SUBJECT",
"D"."STATE" AS "STATE",
"D"."DATEINCOME" AS "DATEINCOME",
"D"."DATESEND" AS "DATESEND",
"D"."ID_EMAILACCOUNT" AS "ID_EMAILACCOUNT",
"D"."DBID_EMAILACCOUNT" AS "DBID_EMAILACCOUNT",
"F"."GESCHST" AS "GESCHST",
"F"."KEYTYP" AS "KEYTYP",
"H"."NAME" AS "NAME"
FROM "CONTACTUSER" AS "C"
INNER JOIN "CONTACT" AS "D" ON ("C"."ID_CONTACT" = "D"."ID") AND ("C"."DBID_CONTACT" = "D"."DBID")
LEFT OUTER JOIN "DBLINK" AS "F" ON ("D"."ID_DBLINK" = "F"."ID") AND ("D"."DBID_DBLINK" = "F"."DBID")
INNER JOIN "EMAILACCOUNT" AS "H" ON ("D"."ID_EMAILACCOUNT" = "H"."ID") AND ("D"."DBID_EMAILACCOUNT" = "H"."DBID")
WHERE ((0 = "D"."ID_DBLINK") OR ("D"."ID_DBLINK" IS NULL)) OR ("F"."MANDNR" = @p__linq__0)
SQL Server (DBLINK での複数の JOINS、非常に遅い):
SELECT
"C"."ID_CONTACT" AS "ID_CONTACT",
"C"."READDATE" AS "READDATE",
"C"."DBID_CONTACT" AS "DBID_CONTACT",
"F"."DATEINSERT" AS "DATEINSERT",
"F"."FROMUSER" AS "FROMUSER",
"F"."FILENAME" AS "FILENAME",
"F"."KIND" AS "KIND",
"F"."MSGID" AS "MSGID",
"F"."ID_DBLINK" AS "ID_DBLINK",
"F"."DBID_DBLINK" AS "DBID_DBLINK",
"F"."SUBJECT" AS "SUBJECT",
"F"."STATE" AS "STATE",
"F"."DATEINCOME" AS "DATEINCOME",
"F"."DATESEND" AS "DATESEND",
"F"."ID_EMAILACCOUNT" AS "ID_EMAILACCOUNT",
"F"."DBID_EMAILACCOUNT" AS "DBID_EMAILACCOUNT",
"J"."GESCHST" AS "GESCHST",
"L"."KEYTYP" AS "KEYTYP",
"N"."NAME" AS "NAME"
FROM "CONTACTUSER" AS "C"
INNER JOIN "CONTACT" AS "D" ON ("C"."DBID_CONTACT" = "D"."DBID") AND ("C"."ID_CONTACT" = "D"."ID")
LEFT OUTER JOIN "CONTACT" AS "F" ON ("C"."DBID_CONTACT" = "F"."DBID") AND ("C"."ID_CONTACT" = "F"."ID")
LEFT OUTER JOIN "DBLINK" AS "H" ON ("F"."DBID_DBLINK" = "H"."DBID") AND ("F"."ID_DBLINK" = "H"."ID")
LEFT OUTER JOIN "DBLINK" AS "J" ON ("F"."DBID_DBLINK" = "J"."DBID") AND ("F"."ID_DBLINK" = "J"."ID")
LEFT OUTER JOIN "DBLINK" AS "L" ON ("F"."DBID_DBLINK" = "L"."DBID") AND ("F"."ID_DBLINK" = "L"."ID")
LEFT OUTER JOIN "EMAILACCOUNT" AS "N" ON ("F"."DBID_EMAILACCOUNT" = "N"."DBID") AND ("F"."ID_EMAILACCOUNT" = "N"."ID")
WHERE ((0 = "D"."ID_DBLINK") OR ("F"."ID_DBLINK" IS NULL)) OR ("H"."MANDNR" = @p__linq__0)
コードの最初のデータは次のとおりです。
modelBuilder.Entity<CONTACT>()
.HasOptional(b => b.DBLINK)
.WithMany()
.HasForeignKey(b => new { b.ID_DBLINK, b.DBID_DBLINK });
modelBuilder.Entity<CONTACT>()
.HasKey(d => new { d.ID, d.DBID })
.HasMany(d => d.CONTACTUSER)
.WithRequired(d => d.CONTACT)
.HasForeignKey(l => new { l.ID_CONTACT, l.DBID_CONTACT });
modelBuilder.Entity<CONTACT>()
.HasRequired(b => b.EMAILACCOUNT)
.WithMany()
.HasForeignKey(b => new { b.ID_EMAILACCOUNT, b.DBID_EMAILACCOUNT });
modelBuilder.Entity<CONTACT>().HasKey(a => new { a.ID, a.DBID });
modelBuilder.Entity<DBLINK>().HasKey(a => new { a.ID, a.DBID });
modelBuilder.Entity<CONTACTUSER>().HasKey(a => new { a.ID, a.DBID });
modelBuilder.Entity<EMAILACCOUNT>().HasKey(a => new { a.ID, a.DBID });
base.OnModelCreating(modelBuilder);
}
私は今何日も探しています。同じコードで異なる SQL ステートメントを生成するにはどうすればよいですか?
- IIS版(開発PC:IIS8、サーバーIIS7.5)
- どちらの環境も同じデータベース サーバー (Firebird 2.5) を使用しています。
- プロジェクト全体をサーバーにコピーしましたが、変更はありません...
- プロジェクトは.NET 4.5に設定されています
- この関数を使用してサンプルの WPF-Projekt を作成したところ、サーバーで RIGHT SQL を使用した結果になりました
- FirebirdSql.Data.FirebirdClient.dll バージョン 3.0.2.0 EntityFramework.dll 5.0.0
このようにクエリの作成に影響を与える可能性のあるものは何か分かりますか?