Entity Framework로 MySQL 접속하기

Entity Framework로 MySQL 접속하기

Visual Studio 2017 & Windows 10 을 기준으로 검증한 내용입니다.

0. Install-Package EntityFramework

Entity Framework가 인스톨 되어 있지 않았을 경우엔 EntityFramework 6.0 을 설치합니다.

1. MySQL for Visual Studio를 설치

「mysql-for-visualstudio-2.0.5.msi」

Download MySQL for Visual Studio https://dev.mysql.com/downloads/windows/visualstudio/2.0.html

2. MySQL connector를 설치

「mysql-connector-net-6.9.11.msi」

다운로드 미러 http://mirror.cogentco.com/pub/mysql/MySQLInstaller/

3. NuGet 패키지 관리에서 MySql.Data, MySql.Data.Entity, MySql.Web을 설치

Nuget 패키지 설치시에는 확실한 버젼 관리를 위해 Package Manager Console를 사용해서 인스톨 합니다.

Windows 10 & Visual Studio 2017 조합의 경우엔 버젼이 모두 일치하지 않으면 Entity Framework의 Data Wizard가 정상적으로 동작하지 않습니다.

DB접속시에 설정창이 갑자기 닫히는 오류 발생한다면 MySQL connector와 MySql.Data, MySql.Data.Entity, MySql.Web의 버젼 중에 어느 하나가 다를 가능성이 있으므로 버젼을 확인해서 모두 같은 버젼이 설치되도록 해 줍니다.

그렇기 때문에 여러명이서 프로젝트를 수행하는 경우엔 사전에 팀 전원의 MySQL Connector 버젼을 일치시킬 필요가 있습니다.

또한 오래전에 개발하고 간만에 손볼 필요가 있는 프로젝트의 DB요소를 변경하고 싶을 경우엔 개발하고 있는 PC의 MySQL Connector 버젼에 맞춰서 패키지들을 갱신해 줄 필요가 있습니다.(반드시 Package Manager Console에서 버젼을 지정해서 갱신합니다)

버젼 불일치시의 DB접속 오류에 대한 자세한 내용은 아래 사이트를 참고 부탁드립니다. Entity Framework’s Entity Data Wizard Crashes When Connecting to MySQL Database

4. config의 수정

3까지의 단계에서 Web.config (또는 App.config) 가 자동으로 수정됩니다.

entityFramework태그의 디폴트 설정인 상태로는 MySQL에 접속이 되지 않기 때문에 아래와 같이 수정 합니다.

또한 connectionString태그를 각자 설정에 맞게 별도 수정 할 필요가 있습니다.

「App.config」나 「Web.config」에 있는 Entity Framework의 디폴트 태그를 삭제합니다.

<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>

config에 아래의 Entity Framework태그를 추가합니다.

<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>

5. 프로젝트를 빌드

6. ADO.NET Entity Data Model을 추가


아래는 Visual Studio 2015 & Windows 7 을 기준으로 검증한 내용입니다.

1. 필수조건

다운로드 미러

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(참조) 추가

4. 프로젝트를 빌드

4. 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

참고

Entity Framework 6 + MySQL 5.6 でADO.NET Entity Data Model デザイナを使う手順

1. mysql-visualstudio-plugin-1.1.1.msi의 인스톨

2. mysql-connector-net-6.8.3.msi의 인스톨

3. NuGet 패키지 관리에서 아래 내용을 인스톨

3까지의 단계에서 Web.config (또는 App.config) 가 자동으로 수정된다.
connectionString태그를 각자 설정에 맞게 수정.

다운로드

참고 사이트

데이터 모델 작성시에 자주 발생하는 에러

호환성 에러가 발생할 경우의 해결책

2018/06/14

Win10 & VS2017 환경기준.

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가 정상적으로 동작하지 않는다.(DB접속시에 설정창이 갑자기 닫히는 오류 발생)

그렇기 때문에 여러명이서 프로젝트를 수행하는 경우엔 사전에 팀 전원의 MySQL Connector 버젼을 일치시킬 필요가 있다.

그리고 오래전에 개발하고 간만에 손볼 필요가 있는 프로젝트의 DB요소를 변경하고 싶을 경우엔 개발하고 있는 PC의 MySQL Connector 버젼에 맞춰서 패키지들을 갱신해 줄 필요가 있다.(확실한 버젼 관리를 위해 Package Manager Console를 사용해서 갱신하자)

나의 경우엔 이번에 2년만에 아래와 같이 갱신했다.

운용서버와 테스트용 서버의 windows 서버에 인스톨 되어있는 MySQL Connector를 확인하고 적절하게 버젼을 갱신하자.

그리고 당연히 버젼을 갱신한 다음에는 테스트용 서버에서 기존 기능의 동작확인도 필수.

더 자세한 사항은 아래 링크를 참고

https://stackoverflow.com/questions/39110060/entity-frameworks-entity-data-wizard-crashes-when-connecting-to-mysql-database

Pie's Tech Note

생계형 개발자의 메모장

comments powered by Disqus

    rss facebook twitter github youtube mail spotify instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora