0

我正在为 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; }
    }

搜索了一段时间后,我找到了这个解决方案并将其转换为用于 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 的指示下,它奏效了!对于那些想要查看代码的人来说,这里是 repo:https ://github.com/Tahirhan/RhinoPluginSqlCECodeFirst

谢谢!

4

2 回答 2

1

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

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

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

接着:

[DbConfigurationType(typeof(SqlCeDbConfiguration))]
public class ModelSqlCECodeFirst : DbContext
于 2021-01-14T10:14:34.240 回答
0

正如这个答案所建议的那样,对于 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)));
    }
}
于 2021-01-14T11:40:06.947 回答