10

EF 4.1を使用しています。通常のEF edmxファイルを作成しています。 DBから生成します。Entity Frameworkで作成されるDbContextモデルを使用できません

生成されたら、右クリックしてコード生成項目を追加して新しいクラスを生成し、代わりにDbContextを使用します。テンプレートDbContextジェネレータを使用します。

すべて正常です。私は、コンテキストの新しいインスタンスを作成する問題はありませんが、私はそれを問題を照会しようとしたときに発生

using (var context = new PasDBEntities()) 
{ 
    var client=context.ClientCompanies.SingleOrDefault(_=>_.ID==clientCompanyId); 
    if(client!=null) 

は、それから私は、コンテキストを照会するためにトライ。私はUnintentionalCodeFirstExceptionに固執します。 し、エラーを取得します。

{"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."}

私は最初のコードを使用したくないが、私はそれをオフに「切り替える」ことができれば、私は知らない、または問題がどこにありますか。参考のため

は、ここに私のコンストラクタです...

public partial class PasDBEntities : DbContext 
{ 
    public PasDBEntities() 
     : base("PasDBEntities") 
    { 
    } 

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

...と接続文字列:

<connectionStrings> 
    <add name="PasDBEntities" 
     connectionString="metadata=res://*/PasDB.csdl| 
            res://*/PasDB.ssdl| 
            res://*/PasDB.msl; 
          provider=System.Data.SqlClient; 
          provider connection string=&quot; 
          data source=localhost; 
          initial catalog=PasDB; 
          integrated security=True; 
          pooling=False; 
          multipleactiveresultsets=True; 
          App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" /> 
</connectionStrings> 
+6

は(例外を引用する)「**アプリケーションを実行**の設定ファイル」で、この接続文字列であり、だけでなく、設定ファイルの:

あなたの接続文字列でなければなりません図書館プロジェクトの – Slauma

+1

あなたはそうです。私はEFを使用できるように私のユニットテストプロジェクトでconnectionstring用の追加の設定ファイルを追加しなければならないことを知らなかった。それは私の問題を解決し、別のapp.configファイルを追加しました。 – Fore

答えて

7

私はあなたが生成するためのテンプレートとEDMX(.tt)を使用している参照しますクラス。しかし、既存のデータベースから情報を取得する場合、ウィザードはObjectContextと互換性のあるConnectionString(メタデータ情報とentityframeworkのプロバイダ)を作成します。

問題は、使用している接続文字列がObjectContext(データベースファーストとモデルファースト)用であることです。 DbContextの場合、メタデータ情報なしでconnectionstringを使用する必要があります。

<connectionStrings> 
<add name="PasDBEntities" 
    connectionString="data source=localhost; 
         initial catalog=PasDB; 
         integrated security=True; 
         pooling=False; 
         multipleactiveresultsets=True; 
         App=EntityFramework" 
    providerName="System.Data.SqlClient" /> 

+1

実際にEF5では、edmxとDbContextとのエンティティ接続を使用できるはずです。 VS2012では、T4はDatabaseFirstのDbContextを生成するように変更されました。 – Pawel

+1

私は実際にコードから最初にデータベースに移動しようとしていました。あなたの答えは、特別なメタデータの埋め込み文字列を使用する必要があることを示しました。基本的には、ADO.NETウィザードを使用するときに、「接続文字列を保存する」というNEEDSを使用することです。 – Worthy7

関連する問題