【发布时间】:2021-01-13 12:18:02
【问题描述】:
我正在为 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
谢谢!
【问题讨论】:
-
解析器类中的代码是否被调用?机器上是否安装了 SQL CE 运行时 MSI?
-
嗨@ErikEJ,我添加了调用解析器的DbConfiguration派生类到代码中,但错误仍然存在。是的,安装了 SQL CE 运行时(来自此链接microsoft.com/tr-tr/download/details.aspx?id=30709,我猜它是要安装的正确包?)。
-
是的,正确的包。
标签: c# entity-framework ef-code-first sql-server-ce rhino3d