2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ASP.NET MVC(C#) EF6+MySqlの環境構築とハマった事

Last updated at Posted at 2016-06-14

環境構築

インストールしたもの

  • mysql-connector-net-6.9.8
  • mysql-for-visualstudio-1.2.6

Nuget経由でインストールしたパッケージ

  • EntityFramework v6.0.0
  • MySql.Data.Entity
  • MySql.Data ・・・MySql.Data.Entityを入れたら入ります

Web.Configの設定

web.config
<add name="testEntities" connectionString="metadata=res://*/test.csdl|res://*/test.ssdl|res://*/test.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=ここにサーバー;user id=ここにID;password=ここにパスワード;persistsecurityinfo=True;database=ここにデータベース名;AllowZeroDateTime=True;ConvertZeroDateTime=True;CharacterSet=utf8&quot;" providerName="System.Data.EntityClient" />

追加したオプション(今回追加したもの)

  • AllowZeroDateTime
  • ConvertZeroDateTime
  • MySqlのDateTime型の初期値は「0000-00-00 00:00:00」
  • .NetのDateTime型の初期値は「0001/01/01 00:00:00」
    上記の変換の際にエラーが起きるのを防ぐ。

    このオプションをTrueにすることで、取得の際に「0001-01-01 00:00:00」この値にコンバートして値を返してくれる。
  • CharacterSet
  • utf8を設定。
    これはクライアント側とDB側で合わせないと文字化けが発生するため設定した。

その他のオプションはこちら

ハマったこと

  • オプションの追加部分を忘れていた為、エラー解消に時間を要した。
  • Mysqlで定義した型が、意図しない型でEF側で生成された。

    例えば、MySql側: TINYINT(1) → .Net側:bool

    この辺りは、マニュアルを熟読して勉強が必要。
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?