2013-12-13 7 views
6

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、しかしそれを取り除いてもそれはまだそこにあります。

ここで何かやっていることがありますか(アセンブリリファレンスなど)がありませんか?

答えて

11

この問題に関するその他の検索を行った後も、this MSDNフォーラムリンクを取得しました。ローワンによって提案されたように、私は手動で私のEFRepositoryクラスに次の文を使用してコンテキストを初期化しようとした:それはGetAll()方法を打つ前に

dbContext.Database.Initialize(false); 

アプリケーションが道を失敗しました。しかし、これは私にいくつかの方向性を与えたスタックトレース公開:DbProviderManifestExtensions.GetStoreTypeFromNameを探して次に

[InvalidOperationException: Sequence contains no matching element] 
    System.Linq.Enumerable.Single(IEnumerable`1 source, Func`2 predicate) +2614017 
    System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName 
         (DbProviderManifest providerManifest, String name) +146 
    .....Other Lines..... 
    System.Data.Entity.Internal.InternalContext.Initialize() +31 
    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType 
                  (Type entityType) +38 
    System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +138 
    System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +38 
    System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable 
         .get_Provider() +99 
    System.Linq.Queryable.Any(IQueryable`1 source) +50 

を、このEFは、列の型を取得しようとしていたラインであることを明らかにしました。私はこれをコメントしたら、すべてが順調だった、

Property(x=> x.Id).HasColumnType("UNIQUEIDENTIFIER") 

:私は私のId列のためUNIQUEIDENTIFIERを指定していました。

requestCodeplexがありますが、カラムタイプがデータベースプロバイダにとって有効でない場合に適切なエラーメッセージを提供します。

+0

あなたは私の心を救います!私はそれに夢中だった! – wilver

+0

SQL CEで 'Property(d => d.BTCValue).HasPrecision(24,8)'メソッドを呼び出したときと同じ例外がありました。理由を明確にしてくれてありがとう! – Andras

+0

日付、smalldateとテキストがあまりにも良く適合していないようです。 –