IT業界のすみっこ暮らし

ふと気がついたときの記録



Entity Framework:最新の日付のレコードを抽出

DBサンプル

tb1

Id DetailId LastUpdatedAt
1 101 2017-02-01 01:00:00
2 101 2017-02-01 10:00:00
3 101 2017-02-02 01:00:00
4 102 2017-02-01 01:00:00
5 102 2017-02-02 01:00:00

tb2

DetailId Name
101 Name1
102 Name2

例1

tb1から同じDetailIdを持つ複数のレコードの中で、一番最新の日付のものを抽出する

var list = (from t in db.tb1
           group t by t.DetailId into g
           select g.OrderByDescending(e => e.LastUpdatedAt).FirstOrDefault() into p
           select new 
           {
               Id = p.Id,
               DetailId = p.DetailId,
               LastUpdatedAt = p.LastUpdatedAt
           }).ToList();

例2

tb1から同じDetailIdを持つ複数のレコードの中で、一番最新の日付のもの、且つステータスが10であるもので、tb2と紐付いてるNameも抽出する

var list = (from t in db.tb1
            group t by t.OrderDetailId into g
            select g.OrderByDescending(e => e.LastUpdatedAt).FirstOrDefault() into p
            join tt in db.tb2 on p.DetailId equals tt.DetailId
            where 
                p.Status == 10
           select new 
           {
               Id = p.Id,
               DetailId = p.DetailId,
               Name = tt.Name,
               LastUpdatedAt = p.LastUpdatedAt
           }).ToList();

参考サイト

stackoverflow.com

stackoverflow.com

EntityFrameworkでMySQL接続

qursaan-howto.blogspot.jp

1、必須条件

「mysql-installer-community-5.7.3.0-m13.msi」をインストールするか 「mysql-visualstudio-plugin-1.1.1.msi」と「mysql-connector-net-6.8.3.msi」をインストールする。

ダウンロードミラー http://mirror.cogentco.com/pub/mysql/MySQLInstaller/

2、Entity Framework

「App.config」や「Web.config」にあるentityFrameworkのデフォルトタグを削除する。

<entityFramework>   
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">  
<parameters> 
<parameter value="v11.0" />  
</parameters>  
</defaultConnectionFactory>  
 <providers>  
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />  
</providers>  
</entityFramework>

下記のentityFrameworkタグを追加する。

<entityFramework>  
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" /> 
<providers>  
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />     
</providers>  
</entityFramework>

3、References(参照)追加

MySql.Data.dll
MySql.Data.Entity.EF6.dll
MySql.Web.dll

4、プロジェクトをリビルド

5、ADO.NET Entity Data Modelの追加手順を行う

まとめ1

1, Reinstall MySQL Tools with the latest stable MySQL connector for windows.
2, Add this 3 files as reference in Visual Studio.
3, MySql.Data.dll
4, MySql.Data.Entity.EF6.dll
5, MySql.Web.dll
6, Modify following Web.config from

まとめ2

holidayprogramming.hatenablog.com

mysql-visualstudio-plugin-1.1.1.msiのインストール
mysql-connector-net-6.8.3.msiのインストール
NuGetパッケージの管理で以下をインストール
・EntityFramework (6.1.1)
・Mysql.Data (6.9.3)
・Mysql.Data.Entities (6.8.3.0)
・Mysql.Web (6.9.3)
手順3までの操作で Web.config (あるいはスタンドアロンAPなら App.config) が修正されます。
connectionStringタグを若干修正します。

※ダウンロード先
http://forums.mysql.com/read.php?174,601041,601041 http://dev.mysql.com/downloads/connector/net/6.8.html

参考サイト

Database First - EF6 | Microsoft Docs

データーモデル作成時によく発生するエラー

stackoverflow.com

互換性エラーが出た場合の解決策

qursaan-howto.blogspot.jp


以上

2018/06/14

Win10 & VS2017

stackoverflow.com

I have been fighting this fight for two weeks. I FINALLY found a combo of versions that works for me. The following was applied to a pristine re-image of my Win10 desktop two weeks ago. All patches were applied to system software before starting to deal with MySql.

Just this morning I re-read the post in this thread (MySql Forums): https://forums.mysql.com/read.php?174,659102,660369#msg-660369

I thought that I had tried those suggestions before but I must have messed up one of the versions. I think that poster's basic strategy is sound. In my case I uninstalled all local MySql parts and the installer.

I downloaded MySQL for Visual Studio - the Latest Development version (2.0.5 msi as of this writing) and installed that.

I found the OLD versions of the .Net Connector and installed 6.9.11

In VS 2017 I created a dummy project using .Net 4.6.1.

I applied pending VS updates.

I built the project.

I went to "Project -> Manage Nuget Packages" and installed / downgraded EntityFramework to 6.0.

I installed MySql.Data, MySql.Data.Entity and MySql.Web all at version 6.9.11

I cleaned and built the project then tried to add a new "Code First" model from a MySql DB... IT WORKED!

2018/12/13

Win10 & Visual Studio 2017の組み合わせでは下記のバージョンが全て一致しないとEntityFrameworkによるData Wizardが正常に作動しない。(アクセス時にダイアログが落ちてしまう)

  • MySQL Connectorのバージョン
  • MySql.Data
  • MySql.Data.Entity
  • MySql.Web

よって、複数人が参加するプロジェクトの場合は改めて全員の開発端末のMySQL Connectorを一致させる必要がある。

また、古いプロジェクトのDB Entityを更新する場合は開発端末に合わせてNuget Packageのバージョン更新が必要(必ずPackage Manager Consoleでバージョンを指定してインストールする)。

c# - Entity Framework's Entity Data Wizard Crashes When Connecting to MySQL Database - Stack Overflow





プライバシーポリシー