使用EF6 SQL Server CE代码创建一个数据库

时间:2022-09-15 18:38:12

I am seeing this error when 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:

DBConfiguration:

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

Web.Config:

. 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>

Can't find much about this error. Seems that a connection property is missing on some object, or in some config, or some component I am using is incorrect version.

我找不出这个错误。似乎某个对象、某个配置或我正在使用的某个组件上缺少连接属性。

update

更新

this is pretty crazy.. I've re-created the solution on another machine (windows 7 sp1 with VS 2013) and got all the nuget projects from scratch.

这很疯狂. .我在另一台机器上重新创建了这个解决方案(windows 7 sp1与VS 2013),并从零开始开发了nuget项目。

I bypassed database create problem by creating the database manually, but now started seeing this error:

我通过手工创建数据库绕过了数据库创建问题,但是现在开始看到这个错误:

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

so tried enabling migrations:

所以试着启用迁移:

Enable-Migrations -ContextTypeName MvcTest.Data.AuditDb

which is throwing:

这是扔:

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

it has been a stream of errors with no end in sight..

这是一长串看不到尽头的错误。

has anyone been able to make EF 6 work with SQL CE Code First approach??

有没有人能够让EF 6使用SQL CE代码第一方法?

1 个解决方案

#1


3  

I had the same problem in my Solution. The problem was that I had different versions of EntityFramework in different projects in the solution. My recommendation is that you remove the EntityFramework dll from all the projects, remove the line about it from the packages.config and then reinstall using the "Install-Package EntityFramework" nuget command. Good luck!

我在解决方案中遇到了同样的问题。问题是我在解决方案中的不同项目中有不同版本的EntityFramework。我的建议是从所有项目中删除EntityFramework dll,从包中删除有关它的行。配置,然后使用“安装包EntityFramework”nuget命令重新安装。好运!

#1


3  

I had the same problem in my Solution. The problem was that I had different versions of EntityFramework in different projects in the solution. My recommendation is that you remove the EntityFramework dll from all the projects, remove the line about it from the packages.config and then reinstall using the "Install-Package EntityFramework" nuget command. Good luck!

我在解决方案中遇到了同样的问题。问题是我在解决方案中的不同项目中有不同版本的EntityFramework。我的建议是从所有项目中删除EntityFramework dll,从包中删除有关它的行。配置,然后使用“安装包EntityFramework”nuget命令重新安装。好运!