Creating a database with EF6 SQL Server CE Code First crashing

code-first dbcontext entity-framework entity-framework-6 sql-server-ce

Question

This error appears whenever I run my app.

[MissingMethodException: Method not found: 'System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher System.Data.Entity.Infrastructure.Interception.DbDispatchers.get_Connection()'.]
   System.Data.Entity.SqlServerCompact.SqlCeProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 timeOut, StoreItemCollection storeItemCollection) +0
   System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +75
   System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() +132

Code:

var db = new AuditDb();
db.Database.CreateIfNotExists(); // blows up! 
or
db.Set<CampaignAudit>().Find(0); // same error

References:

EntityFramework:                  6.0.0.0
EntityFramework.SqlServerCompact: 6.0.0.0
System.Data:                      4.0.0.0
System.Data.SqlServerCe:          4.0.0.0

Context:

[DbConfigurationType("MvcTest.Data.AuditDBConfiguration, MvcTest")]
public class AuditDb : DbContext
{
    // tried this approach as well:
    // static AuditDb() { Database.SetInitializer(new CreateDatabaseIfNotExists<AuditDb>()); }
    public AuditDb() : base ("Name=AuditDB"){}
    public DbSet<CampaignAudit> Audits { get; set; }

    protected override void OnModelCreating(DbModelBuilder builder)
    {

        Map<CampaignAudit>(builder);
        ...
}

DBConfiguration:

public class AuditDBConfiguration : DbConfiguration
{
    public AuditDBConfiguration()
    {
        SetProviderServices(
            SqlCeProviderServices.ProviderInvariantName,
            SqlCeProviderServices.Instance
        );
        SetDefaultConnectionFactory(
            new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName)
        );
    }
}

Web.Config:

<add name="AuditDB" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=C:\logs\GroundAuditDb.sdf" />

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
<DbProviderFactories>
  <remove invariant="System.Data.SqlServerCe.4.0" />
  <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>

Finding little information regarding this mistake. It appears that some object, some configuration, or some component I'm using doesn't have a connection attribute, or that it has the wrong version.

update

This is really bizarre. On a different computer running Windows 7 SP1 and Visual Studio 2013, I recreated the solution from start and obtained all the nuget projects.

By manually building the database, I was able to get around the database creation issue, but now I'm getting the following error:

Could not load type 'System.Data.Entity.Migrations.Model.AlterTableOperation' from assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

therefore I attempted to enable migrations:

Enable-Migrations -ContextTypeName MvcTest.Data.AuditDb

who is hurling:

Method not found: 'Void System.Data.Entity.ModelConfiguration.Configuration.EntityMappingConfiguration`1.MapInheritedProperties()

There has been an endless string of mistakes.

Has EF 6 been successfully integrated with the SQL CE Code First technique by anyone?

1
2
5/25/2014 6:27:00 PM

Popular Answer

The issue was present in my solution as well. The issue was that I used several EntityFramework versions in various projects for the solution. My advice is to uninstall EntityFramework from all projects, delete the reference to it from packages.config, and then reinstall using the "Install-Package EntityFramework" nuget command. Good fortune!

3
5/26/2014 11:00:51 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow