'3. Implementation/Entity Framework'에 해당되는 글 3건

  1. 2013.09.26 Entity Framework and MySQL
  2. 2013.08.20 Linking Table 이 Entity 가 아닌 Association 으로 잡히는 문제 해결
  3. 2013.08.20 Database First
2013. 9. 26. 05:59

Entity Framework and MySQL

Install the Provider

In order to connect to MySQL databases, we will need to install an appropriate ADO.NET and Entity Framework provider. Luckily, the provider we're using is available via NuGet.

  1. Inside PMC, run Install-Package MySQL.Data.Entities

We also need to register the provider. Open App.config, and anywhere inside the configuration element, add the following fragment.

<system.data>
  <DbProviderFactories>
    <add name="MySQL Data Provider"
         invariant="MySql.Data.MySqlClient"
         description="Data Provider for MySQL"
         type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
  </DbProviderFactories>
</system.data>

Add a Connection String

In order to connect to the right database, we need to add a connection string to the App.Config. Anywhere inside the configuration element, add the following fragment.

<connectionStrings>
  <add name="ChinookContext"
        connectionString=
"server=localhost;database=Chinook;User Id=root;password=P4ssw0rd"
        providerName="MySql.Data.MySqlClient" />
</connectionStrings>


참고:


http://brice-lambson.blogspot.kr/2012/10/entity-framework-on-mysql.html

http://www.saotn.org/mysql-connector-net-entity-framework/

http://www.saotn.org/mysql-connector-net-6-6-4-0-partial-trust/

2013. 8. 20. 07:41

Linking Table 이 Entity 가 아닌 Association 으로 잡히는 문제 해결

STUDENTS 와 CLASSES 테이블이 있습니다. 

그리고 학생과 반의 다대다 관계를 해결하기 위한 STUDENTCLASSES Linking Table 이 있습니다.


이 3 테이블의 Diagram 은 아래와 같습니다.





여기서 주목해야 할 점은 Linking Table 인 STUDENTCLASSES 에는 각 참조하는 Table 의 Foreign Key 로만 구성된 Composite Primary Key 만 존재합니다. 


이 구조에서 Entity Framework 를 이용해서 Model 을 가져오면 아래와 같습니다.





STUDENTCLASSES 가 Entity 가 아닌 Assocations 로 잡힌 걸 볼 수 있습니다. 이렇게 모델을 가져오는 건 Entity Framework 의 원래 동작입니다. 만약 이 관계에서 STUDENTCLASSES 를 Entity 로 인식하게 하려면 RegisteredDate 와 같은 Column 하나를 추가하면 됩니다.




이렇게 정의 한 후 다시 Entity Framework Model 을 가져오면 아래와 같이 Linking Table 의 Association 이 아닌 Entity 로 인식되게 됩니다.




주의 사항은 저렇게 테이블 정의를 변경하더라도, "Update Model From Database" 에서는 자동으로 반영되지 않습니다. (이 기능에 대해서 신뢰성이 의심가는 부분 ... ㅡㅡ;) 다시 Model 을 생성해 주어야만 올바르게 Entity 로 인식됩니다.



2013. 8. 20. 07:12

Database First

MS 에서 제공하는 "Database First" Entity Framework 사용법입니다. 이래저래 설치할 게 많네요. 그래도 설정이 끝난 후에는 DB 개발이 한결 편리해 진 것 같습니다. 근데 LINQ 에 적응이 안되는 건 ... 쩝.


http://msdn.microsoft.com/en-us/data/jj206878


Visual Studio 2010 기준 설치해야 될 Extension 을 정리하면 다음과 같습니다. 


* EF 5.x DbContext Generator for C#


* Entity Framework Power Tools Beta 3


* NuGet Package Manager





그리고, Entity Model 을 생성한 프로젝트에서


솔루션 탐색기 > 프로젝트 선택 > 오른쪽 메뉴 > "Manage NuGet Packages ..." 를 선택 후,





Entity Framework 를 또 설치해 주어야만 빌드할 수 있습니다.





2012 에서는 좀 더 편한 것 같은데 2010 에서는 절차가 복잡하네요~