在不添加 App.Config 的情况下将实体框架与 Sql Server Compact Edition 一起使用

问题描述

我正在为 Rhinoceros 6 开发一个插件,目前看来无法对 Rhinoceros 的 App.Config 文件进行编辑。插件项目的App.Config对Rhinoceros的App.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 类:

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

搜索一段时间后,我找到了 this solution 并将其转换为用于 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 框架 4.8 -EntityFramework 6.4.4 -SqlServerCe.4.0

使用 ErikEJ 的指令,它起作用了!对于那些想要查看代码的人,这里是 repo: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
,

正如 this answer 建议的那样,对于 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)));
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...