DbContext not initializing with SQL Server Compact in ASP.Net MVC

Viewed 7300

I have created a simple project using ASP.Net MVC template in Visual Studion 2013 Express for Web. It does not use any authentication. Then I installed EntityFramework (v6.0.1), EntityFramework.SqlServerCompact packages.

My DbContext class is very simple:

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());
    }
}

The actual context object is created in the Unit of Work class:

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);
        }
    }
}

The connection string being used is:

<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" />

Now when I try to access any thing using this Context like:

var rep=UoW.EditTestRepo;
var list=rep.GetAll().ToList();

I am getting following exception on rep.GetAll() function:

System.InvalidOperationException: Sequence contains no matching element

On digging deeper, IQueryable from Repository class (DbSet<EditTest>) is throwing following exception:

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.

I thought it might have been caused by ninject, but it is still there even after I removed it.

What I am doing wrong here or something (some assembly reference etc.) is missing?

1 Answers
Related