c# - 无法在 Unity 中为 SQLite 配置实体框架
问题描述
我想在 Unity 中使用使用实体框架的 SQLite 数据库。但是在运行时会发生错误:
NotSupportedException:无法确定类型为“System.Data.SQLite.SQLiteConnection”的连接的 DbProviderFactory 类型。确保在应用程序配置中安装或注册了 ADO.NET 提供程序。
这是我的实体框架配置:
class SqliteDbConfiguration : DbConfiguration
{
public SqliteDbConfiguration()
{
string assemblyNameEF6 = typeof(SQLiteProviderFactory).Assembly.GetName().Name;
RegisterDbProviderFactories<SQLiteProviderFactory>("SQLite Data Provider (Entity Framework 6)", ".NET Framework Data Provider for SQLite (Entity Framework 6)");
RegisterDbProviderFactories<SQLiteFactory>("SQLite Data Provider", ".NET Framework Data Provider for SQLite");
SetProviderFactory(assemblyNameEF6, SQLiteFactory.Instance);
SetProviderFactory(assemblyNameEF6, SQLiteProviderFactory.Instance);
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
DbProviderServices provider = (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices));
SetProviderServices(assemblyNameEF6, provider);
SetProviderServices("System.Data.SQLite", provider);
SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("mssqllocaldb"));
AddDependencyResolver(new SingletonDependencyResolver<DbProviderFactory>(SQLiteProviderFactory.Instance));
}
static void RegisterDbProviderFactories<T>(string name, string description)
{
string assemblyName = typeof(T).Assembly.GetName().Name;
var dataSet = ConfigurationManager.GetSection("system.data") as DataSet;
if (dataSet != null)
{
var dbProviderFactoriesDataTable = dataSet.Tables.OfType<DataTable>()
.First(x => x.TableName == typeof(DbProviderFactories).Name);
var dataRow = dbProviderFactoriesDataTable.Rows.OfType<DataRow>()
.FirstOrDefault(x => x.ItemArray[2].ToString() == assemblyName);
if (dataRow != null)
{
dbProviderFactoriesDataTable.Rows.Remove(dataRow);
}
dbProviderFactoriesDataTable.Rows.Add(name, description, assemblyName, typeof(T).AssemblyQualifiedName);
}
}
}
类的Click()
方法DBConnection
创建数据库上下文MyDbContext
并尝试向其写入有关的信息Book
。此方法分配给单击按钮,执行时发生错误(在此行:
db.Books.Add(book1);
完整代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data.Entity;
using SQLite.CodeFirst;
using System.Configuration;
using System.Data.SQLite;
using System.Data.Common;
public class DBConnection : MonoBehaviour
{
// Use this for initialization
void Start () {}
public void Click()
{
using (DbConnection connection = new SQLiteConnection("FullUri=file::memory:"))
{
connection.Open();
using (var db = new MyDbContext(connection))
{
Book book1 = new Book { Name = "Граф Монтекристо", Price = 123 };
db.Books.Add(book1);
db.SaveChanges();
}
}
}
// Update is called once per frame
void Update () {}
}
[DbConfigurationType(typeof(SqliteDbConfiguration))]
public class MyDbContext : DbContext
{
private static readonly ConnectionStringSettings connectionString = new ConnectionStringSettings("localDataBase", "data source=.\\db.bytes", "System.Data.SQLite");
static MyDbContext()
{
ConfigurationManager.ConnectionStrings.Add(connectionString);
Configuration exeConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
}
public MyDbContext(DbConnection connection) : base(connection, false)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDbContext>(modelBuilder);
Database.SetInitializer(sqliteConnectionInitializer);
}
public DbSet<Book> Books { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
执行时发生异常DBConnection.Click()
:
NotSupportedException:无法确定类型为“System.Data.SQLite.SQLiteConnection”的连接的 DbProviderFactory 类型。确保在应用程序配置中安装或注册了 ADO.NET 提供程序。
System.Data.Entity.Infrastructure.Net40DefaultDbProviderFactoryResolver+<>c__DisplayClass5.b__0 (System.Type t) (at :0)
System.Collections.Concurrent.ConcurrentDictionary
2[TKey,TValue].GetOrAdd (TKey key, System.Func
2[T,TResult] valueFactory) (at :0)System.Data.Entity.Infrastructure.Net40DefaultDbProviderFactoryResolver.GetProviderFactory(System.Data.Common.DbConnection 连接,System.Collections.Generic.IEnumerable`1[T] dataRows)(在:0)
System.Data.Entity.Infrastructure.Net40DefaultDbProviderFactoryResolver.ResolveProviderFactory(System.Data.Common.DbConnection 连接)(在:0)
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderFactory(System.Data.Common.DbConnection 连接)(在:0)
System.Data.Entity.Core.Common.DbProviderServices.GetProviderFactory(System.Data.Common.DbConnection 连接)(在:0)
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInvariantName(System.Data.Common.DbConnection 连接)(在:0) System.Data.Entity.Internal.InternalConnection.get_ProviderName()(在:0) System.Data.Entity。 Internal.LazyInternalContext.get_ProviderName () (at :0) System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create (System.Data.Entity.DbContext 上下文) (at :0) System.Data.Entity.Internal.LazyInternalContext.InitializeContext ( )(在:0)System.Data.Entity.Internal.InternalContext.Initialize()(在:0)System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(System.Type entityType)(在:0)System.Data。 Entity.Internal.Linq.InternalSet
1[TEntity].Initialize () (at <f8fadb18f6a84dcf8e300a2f11995d19>:0) System.Data.Entity.Internal.Linq.InternalSet
1[TEntity].get_InternalContext () (at :0) System.Data.Entity.Internal.Linq.InternalSet1[TEntity].ActOnSet (System.Action action, System.Data.Entity.EntityState newState, System.Object entity, System.String methodName) (at <f8fadb18f6a84dcf8e300a2f11995d19>:0) System.Data.Entity.Internal.Linq.InternalSet
1[TEntity].Add (System.Object entity) (at :0) System.Data.Entity.DbSet1[TEntity].Add (TEntity entity) (at <f8fadb18f6a84dcf8e300a2f11995d19>:0) DBConnection.Click () (at Assets/Scripts/PersonalCabinet/DBConnection.cs:21) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction
1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem /ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()
解决方案
使用Entity FrameWork Core(用于跨平台)而不是使用Entity FrameWork(不支持.NET Core)进行统一项目;还要确保统一 api 级别是.net 标准 2.0
推荐阅读
- python - 在 python 中使用逆字典简化代码
- r - 增加折线长度 r sf
- django - Django:测试 LoginView 抛出 AssertionError
- sql - SQL 联接而不是 UNION
- gridview - 如何访问 Yii2 GridView ActionColumn 中的行号?
- java - 递归函数倒数4-> 0,然后倒数0> 4,如何停止倒数
- amazon-web-services - 如何从 S3 预签名 URL (Amazon Feed API) 下载 Feed 处理报告
- c# - 从“WinRT.IInspectable”到“System.Collections.Generic.IEnumerable`1[Windows.Storage.IStorageFile]”的无效转换
- reactjs - 错误:使用地图反应时,对象作为 React 子项无效
- kubernetes - 将 Kubernetes Ingress 暴露给 LAN 计算机