22

포기합니다. 찾았습니다. http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx 그리고 멋지다고 생각했습니다. 그래서 두 모델의 장점을 최대한 활용하기 위해 모델을 신속하게 재 설계했습니다.DbContext에서 첫 번째 모델, 새 데이터베이스를 초기화하지 못함

그러나 지금 내 모델은 새 데이터베이스를 만들거나 기존 테이블에 테이블을 추가하지 못합니다.

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

에서 : 이 오류 얻을

 protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     throw new UnintentionalCodeFirstException(); 
    } 

을 그리고 내 연결 문자열입니다 :

<add name="ForumContextContainer" 
providerName="System.Data.SqlClient" 
connectionString="Data Source=.\SQLExpress; Initial Catalog=iForum; Integrated Security=True"/> 

참고. 초기 카탈로그를 나중에 추가했는데 작동하려면 시도했지만 정확히 동일했습니다.

답변

32

잘못된 연결 문자열입니다. 모델 우선/데이터베이스 우선 (EDMX)을 사용하고 나면 .ssdl, .msl 및 .csdl 메타 데이터 파일을 참조 할 때 Entity connection string을 사용해야합니다. 또한 EDMX에서 모델을 만들 때 디자인 타임에 데이터베이스를 만들어야한다는 것을 기억하십시오. = SQL 스크립트를 생성하고 데이터베이스를 생성하기 위해 실행해야합니다.

+0

실제로 T4 템플릿을 chaning하여 주변을 발견했습니다. 어쨌든 덕분에 –

+14

@ Łukasz Baran이 해결책을 공유해 주시겠습니까? –

+0

@JoelBeckham 내 문제는 새로운 UnintentionalCodeFirstException(); 던졌습니다. Entities 클래스에서. – Saber

2

누군가가 EDMX/db를 솔루션의 클래스 라이브러리에 처음 추가하면이 오류가 발생하는 것 같습니다. 그렇게하면 클래스 라이브러리 프로젝트의 App.config 파일에 추가 된 연결 문자열을 web.config 또는 exe 프로젝트 구성 파일에서 사용할 수 있으므로 복사/붙여 넣기 만하면됩니다.

+0

이 간단한 해결책은 다른 모든 것이 실패했을 때 나를 위해 일했습니다. 모델 dll 프로젝트의 app.config에서 MVC app web.config로 연결 문자열 섹션을 복사해야했습니다. – Thronk

0
Add this connection string to web config and make changes: 

<add name="Entities" 
connectionString=" 
metadata=res://*/EFmodel.csdl|res://*/EFmodel.ssdl|res://*/EFmodel.msl; 
provider=System.Data.SqlClient;provider 
connection string=&quot; 
data source=SAI-PC; 
initial catalog=OrderDB; 
user id=sa; 
password=Pass$123; 
MultipleActiveResultSets=True; 
App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" /> 

EFmodel is my .edmx file name. 

OrderDB is database name. 
-1

삭제하거나 주석 :

//protected override void OnModelCreating(DbModelBuilder modelBuilder) 
//{ 
// throw new UnintentionalCodeFirstException(); 
//} 

을 그리고 유효 하나에 연결 문자열을 변경합니다.