Visual Studio 2013 Express for WebでASP.Net MVCテンプレートを使用して簡単なプロジェクトを作成しました。認証を使用しません。その後、EntityFramework
(v6.0.1)、EntityFramework.SqlServerCompact
パッケージをインストールしました。DbContextがASP.Net MVCのSQL Server Compactで初期化されない
マイDbContextクラスは非常に単純です:
public class EditTestContext : DbContext
{
public EditTestContext() : base("EditTestContext")
{
}
public EditTestContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(
new DropCreateDatabaseIfModelChanges<EditTestContext>());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new EditTestConfig());
}
}
実際のコンテキストオブジェクトは、ワーク・クラスの単位で作成されます。
public class EditTestUoW:IEditTestUoW,IDisposable
{
private DbContext dbContext;
public EditTestUoW()
{
CreateDbContext();
}
private void CreateDbContext()
{
dbContext = new EditTestContext();//NEW DBCONTEXT OBJECT IS CREATED
dbContext.Configuration.LazyLoadingEnabled = false;
dbContext.Configuration.ProxyCreationEnabled = false;
dbContext.Configuration.ValidateOnSaveEnabled = false;
}
public IRepository<EditTestModel> EditTestRepo
{
get
{
return new EFRepository<EditTestModel>(dbContext);
}
}
}
使用されている接続文字列は次のとおりです。
<add name="EditTestContext" connectionString="Data Source=
|DataDirectory|EditTestDb.sdf;Max Database Size=256;
Max Buffer Size=1024;File Mode=Shared Read;
Persist Security Info=False;" providerName="System.Data.SqlServerCe.4.0" />
今、このコンテキストを使用して任意のものにアクセスしようとすると、
var rep=UoW.EditTestRepo;
var list=rep.GetAll().ToList();
私はrep.GetAll()
機能に以下の例外取得しています:
のSystem.InvalidOperationExceptionを:シーケンスが深く掘るには一致する要素
を含まない、リポジトリクラス(DbSet<EditTest>
)からIQueryable
は、次の例外がスローされます。
The context cannot be used while the model is being created. This exception may
be thrown if the context is used inside the OnModelCreating method or if the same
context instance is accessed by multiple threads concurrently. Note that instance
members of DbContext and related classes are not guaranteed to be thread safe.
ninject
、しかしそれを取り除いてもそれはまだそこにあります。
ここで何かやっていることがありますか(アセンブリリファレンスなど)がありませんか?
あなたは私の心を救います!私はそれに夢中だった! – wilver
SQL CEで 'Property(d => d.BTCValue).HasPrecision(24,8)'メソッドを呼び出したときと同じ例外がありました。理由を明確にしてくれてありがとう! – Andras
日付、smalldateとテキストがあまりにも良く適合していないようです。 –