将 Entity Framework 与 Sql Server Compact Edition 一起使用,无需添加 App.Config

Using Entity Framework with Sql Server Compact Edition without App.Config additions

我正在为 Rhinoceros 6 开发一个插件,目前看来不可能对 Rhinoceros 的 App.Config 文件进行编辑。 App.Config插件项目对RhinocerosApp.Config没有影响

出现以下错误消息是因为我无法将提供程序和参数添加到 App.Config 的 entityFramework 部分。

Unable to determine the provider name for provider factory of type 'System.Data.SqlServerCe.SqlCeProviderFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.

我用 NuGet 安装了 EntityFramework.SqlServer.Compact 和 Microsoft.SqlServer.Compact 并检查了引用,一切似乎都很好。

下面是我的代码优先 dbcontext class :

        public class ModelLocalClipper : DbContext
    { 
        public ModelLocalClipper()
            : base(new SqlCeConnection("Data Source="+ Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\MyDatabase.sdf;Persist Security Info=False;"),
  contextOwnsConnection: true)
        {
            Database.SetInitializer<ModelLocalClipper>(new CreateDatabaseIfNotExists<ModelLocalClipper>());
        }
        
        public DbSet<Scene> Scenes { get; set; }
        public DbSet<LocalProject> LocalProjects { get; set; }
    }

    public class Scene
    {
        public int SceneId { get; set; }
        public string Name { get; set; }

        public int LocalProjectId { get; set; }

        [ForeignKey("LocalProjectId")]
        public virtual LocalProject LocalProject { get; set; }
    }

    public class LocalProject
    {
        public int LocalProjectId { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Scene> Scenes { get; set; }
    }

搜索一段时间后,我发现 并将其转换为用于 SqlCe,如下所示,但它也没有帮助

public class SqlCeProviderInvariantName : IProviderInvariantName
        {
            public static readonly SqlCeProviderInvariantName Instance = new SqlCeProviderInvariantName();
            private SqlCeProviderInvariantName() { }
            public const string ProviderName = "System.Data.SqlServerCe.4.0";
            public string Name { get { return ProviderName; } }
        }

    class SqlCeDbProviderFactoryResolver : IDbProviderFactoryResolver
    {
        public static readonly SqlCeDbProviderFactoryResolver Instance = new SqlCeDbProviderFactoryResolver();
        private SqlCeDbProviderFactoryResolver() { }
        public DbProviderFactory ResolveProviderFactory(DbConnection connection)
        {
            if (connection is SqlCeConnection) return SqlCeProviderFactory.Instance;
            if (connection is EntityConnection) return EntityProviderFactory.Instance;
            return null;
        }
    }

    class SqlCeDbDependencyResolver : IDbDependencyResolver
    {
        public object GetService(Type type, object key)
        {
            if (type == typeof(IProviderInvariantName)) return SqlCeProviderInvariantName.Instance;
            if (type == typeof(DbProviderFactory)) return SqlCeProviderFactory.Instance;
            if (type == typeof(IDbProviderFactoryResolver)) return SqlCeDbProviderFactoryResolver.Instance;
            return SqlCeProviderServices.Instance.GetService(type);
        }

        public IEnumerable<object> GetServices(Type type, object key)
        {
            var service = GetService(type, key);
            if (service != null) yield return service;
        }
    }

    class SqlCeDbConfiguration : DbConfiguration
    {
        public SqlCeDbConfiguration()
        {
            AddDependencyResolver(new SqlCeDbDependencyResolver());
        }
    }

版本: -RhinoCommon 6.30.20288.16410 -.NET Framework 4.8 -EntityFramework 6.4.4 -SqlServerCe.4.0

使用 ErikEJ 的指令,它成功了!对于那些想要查看代码的人来说,这里是回购协议:https://github.com/Tahirhan/RhinoPluginSqlCECodeFirst

谢谢!

试试这个(更简单的)方法,我可以使用控制台应用程序来实现它:

public class SqlCeDbConfiguration : DbConfiguration 
{
    public SqlCeDbConfiguration()
    {
        SetProviderServices(
            SqlCeProviderServices.ProviderInvariantName,
            SqlCeProviderServices.Instance);

        SetDefaultConnectionFactory(
            new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName));
    }
}

然后:

[DbConfigurationType(typeof(SqlCeDbConfiguration))]
public class ModelSqlCECodeFirst : DbContext

所建议,对于 SQLite 场景,请尝试此 DbConfiguration 并且不要忘记删除“|DataDirectory|”来自连接字符串的部分

public class SQLiteConfiguration : DbConfiguration
{
    public SQLiteConfiguration()
    {
        SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
        SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
        SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
    }
}