エンティティオブジェクトをローカルオブジェクトに変換して、さらに変換するために使用できるようにしようとしています。
エンティティオブジェクトを変換するために使用しているコードは次のとおりです。
IEnumerable<SystemArea> result = (from sa in CurrentContext.systemarea
select new SystemArea
{
SystemAreaId = sa.SystemAreaId,
SystemAreaCode = sa.SystemAreaCode,
SystemAreaType = sa.SystemAreaType,
SystemAreaDescription = sa.SystemAreaDescription,
SystemAreaCreatedDate = sa.SystemAreaCreatedDate,
SystemAreaUpdateDate = sa.SystemAreaUpdateDate,
SystemAreaStatus = sa.SystemAreaStatus,
Count = sa.systemareafunctionality.Count,
SystemAreaFunctionality = sa.systemareafunctionality.Select(e => new SystemAreaFunctionality { SystemAreaCode =e.SystemAreaCode })
}).ToList();
ここで、カウント変数は、その中に子データがあるかどうかを確認するためのものです。
SystemAreaFunctionalityは、SELECT関数を使用してここで変換しようとしている子オブジェクトですが、常に空白のコレクションです。残りのデータは親オブジェクトに割り当てられていますが、ここで欠落しているのは子テーブルレコードだけです。どこがおかしいのか、助けてください!
生成されたSQL:
SELECT
`Project3`.`C1`,
`Project3`.`SystemAreaId`,
`Project3`.`SystemAreaCode`,
`Project3`.`SystemAreaType`,
`Project3`.`SystemAreaDescription`,
`Project3`.`SystemAreaCreatedDate`,
`Project3`.`SystemAreaUpdateDate`,
`Project3`.`SystemAreaStatus`,
`Project3`.`C3` AS `C2`,
`Project3`.`C2` AS `C3`,
`Project3`.`SystemAreaCode1`
FROM (SELECT
`Project1`.`SystemAreaId`,
`Project1`.`SystemAreaCode`,
`Project1`.`SystemAreaType`,
`Project1`.`SystemAreaDescription`,
`Project1`.`SystemAreaCreatedDate`,
`Project1`.`SystemAreaUpdateDate`,
`Project1`.`SystemAreaStatus`,
1 AS `C1`,
`Project2`.`SystemAreaCode` AS `SystemAreaCode1`,
`Project2`.`C1` AS `C2`,
`Project1`.`C1` AS `C3`
FROM (SELECT
`Extent1`.`SystemAreaId`,
`Extent1`.`SystemAreaCode`,
`Extent1`.`SystemAreaType`,
`Extent1`.`SystemAreaDescription`,
`Extent1`.`SystemAreaCreatedDate`,
`Extent1`.`SystemAreaUpdateDate`,
`Extent1`.`SystemAreaStatus`,
(SELECT
COUNT(1) AS `A1`
FROM `systemareafunctionality` AS `Extent2`
WHERE `Extent1`.`SystemAreaCode` = `Extent2`.`SystemAreaCode`) AS `C1`
FROM `systemarea` AS `Extent1`) AS `Project1` LEFT OUTER JOIN (SELECT
`Extent3`.`SystemAreaCode`,
1 AS `C1`
FROM `systemareafunctionality` AS `Extent3`) AS `Project2` ON `Project1`.`SystemAreaCode` = `Project2`.`SystemAreaCode`) AS `Project3`
ORDER BY
`Project3`.`SystemAreaCode` ASC,
`Project3`.`C2` ASC
JSON出力:
[{"SystemAreaId":1、 "SystemAreaCode": "KIO"、 "SystemAreaType": "KIOSK"、 "SystemAreaDescription": "受付に関連するタスク"、 "SystemAreaCreatedDate": "/ Date(1543421018000)/"、 "SystemAreaUpdateDate ":" / Date(1543421018000)/ "、" SystemAreaStatus ":true、" SystemAreaFunctionality ":[]、" Count ":1}]
PS:オートマッパーや拡張メソッドを提案しないでください。ありがとう!
意見:
MySQL(最新バージョン)をEFで動作させるために、2日かかりました。それは骨の折れる作業でしたが、それどころか、MSSQLを使用したEFは非常にシンプルで実装が簡単です。
私が経験したことの1つは、OracleはMySQLの無料バージョンのサポートを提供することにまったく関心がないため、新しいバージョンのドキュメントがずさんで、不安定な.NETコネクタを提供していることです。
実際の答え:
EFは非常に奇妙な動作をしていたため、EFに子エンティティの子(つまり、SystemAreaFuncionalityの子であるSystemAreaFunctionalityEmployeeRoleMapping)をロードするように要求した場合にのみ、子エンティティ(SystemAreaFunctionality)にデータをロードしました。不要なデータを取ります。
したがって、私のリンククエリは次のようになります。
var result = (from sa in CurrentContext.systemarea
select new SystemArea
{
SystemAreaId = sa.SystemAreaId,
SystemAreaType = sa.SystemAreaType,
Count = sa.systemareafunctionality.Count,
SystemAreaFunctionalities = sa.systemareafunctionality.Select(saf => new SystemAreaFunctionality
{
SystemAreaId = saf.SystemAreaId,
SystemAreaFunctionalityController = saf.SystemAreaFunctionalityController,
SystemAreaFunctionalityAction = saf.SystemAreaFunctionalityAction,
SystemAreaFunctionalityType = saf.SystemAreaFunctionalityType,
SystemAreaFunctionalityEmployeeRoleMappings = saf.systemareafunctionalityemployeerolemapping.Select(saferm => new SystemAreaFunctionalityEmployeeRoleMapping
{
SystemAreaFunctionalityEmployeeRoleMappingId = saferm.SystemAreaFunctionalityEmployeeRoleMappingId,
SystemAreaFunctionalityCreatedDate = saferm.SystemAreaFunctionalityCreatedDate
})
})
}).ToList();
代わりに:
今回はPostgreSQLとnpgsqlコネクタを使用して異なるデータベースで同じlinqクエリ(OPに投稿)を使用してみましたが、驚くべきことにEFは余分な荷物なしで私が望むものを正確に提供します。その上、PostgreSQLはMySQLよりもEFで優れたパフォーマンスを提供します。したがって、PostgreSQLへの切り替えがより良いオプションになると思います。
PS: オープンソースのDBMSを決定する場合は、MySQLに飛び込む前にこれを参照してください:
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加