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="
data source=localhost;
initial catalog=PasDB;
integrated security=True;
pooling=False;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
は(例外を引用する)「**アプリケーションを実行**の設定ファイル」で、この接続文字列であり、だけでなく、設定ファイルの:
あなたの接続文字列でなければなりません図書館プロジェクトの – Slauma
あなたはそうです。私はEFを使用できるように私のユニットテストプロジェクトでconnectionstring用の追加の設定ファイルを追加しなければならないことを知らなかった。それは私の問題を解決し、別のapp.configファイルを追加しました。 – Fore