我尝试从xamarin表单项目(uwp和android)使用的.netstandard库项目中打开sqlite db,我只尝试了uwp项目,但无法打开数据库异常
我尝试使用个人文件夹的路径,而我尝试通过Sqliteconnection打开连接。 完整的项目专家可在此处找到:https://github.com/blndr83/OutlookCalender
internal class DatabaseProvider
{
private static ISessionFactory _sessionFactory;
private static Configuration _configuration;
private static HbmMapping _mapping;
public static ISession OpenSession()
{
//Open and return the nhibernate session
return SessionFactory.OpenSession();
}
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
//Create the session factory
_sessionFactory = Configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static Configuration Configuration
{
get
{
if (_configuration == null)
{
//Create the nhibernate configuration
_configuration = CreateConfiguration();
}
return _configuration;
}
}
public static HbmMapping Mapping
{
get
{
if (_mapping == null)
{
//Create the mapping
_mapping = CreateMapping();
}
return _mapping;
}
}
private static Configuration CreateConfiguration()
{
var configuration = new Configuration();
//Loads properties from hibernate.cfg.xml
configuration.Configure();
IDictionary<string, string> props = new Dictionary<string, string>
{
{ "connection.connection_string", @"Data Source=Calendar.db;FailIfMissing=false;New=false;Compress=true;Version=3"},
{ "connection.driver_class", "NHibernate.Driver.SQLite20Driver" },
{ "dialect", "NHibernate.Dialect.SQLiteDialect" },
{ "connection.provider", "NHibernate.Connection.DriverConnectionProvider" },
{ "show_sql", "false" }
};
configuration.SetProperties(props);
configuration.AddDeserializedMapping(Mapping, null);
return configuration;
}
private static HbmMapping CreateMapping()
{
var mapper = new ModelMapper();
//Add the person mapping to the model mapper
mapper.AddMappings(new List<System.Type> { typeof(EventModelMap) });
//Create and return a HbmMapping of the model mapping in code
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
}
public class Repository : IRepository
{
private readonly ISession _session;
public Repository()
{
_session = DatabaseProvider.OpenSession();
var schemaUpdate = new SchemaUpdate(DatabaseProvider.Configuration);
schemaUpdate.Execute(Console.WriteLine, true);
}
public void Delete<T>(T entity) where T : Entity
{
using (var transaction = _session.BeginTransaction())
{
_session.Delete(entity);
transaction.Commit();
}
}
public T Find<T>(Expression<Func<T,bool>> expression) where T : Entity
{
return _session.QueryOver<T>().Where(expression).SingleOrDefault();
}
public async Task<IList<T>> FindAll<T>(Expression<Func<T, bool>> expression) where T : Entity
{
return await _session.QueryOver<T>().Where(expression).ListAsync();
}
public void Save<T>(T entity) where T : Entity
{
using (var transaction = _session.BeginTransaction())
{
_session.Save(entity);
transaction.Commit();
}
}
public void Update<T>(T entity) where T : Entity
{
using (var transaction = _session.BeginTransaction())
{
_session.Update(entity);
transaction.Commit();
}
}
}
使用实体框架核心解决了问题
答案 0 :(得分:0)
您可以尝试SQLite包装器的.NET Standard实现,有两个区别:
提供一个.NET Standard库,该库位于三个主要平台上:UWP,Android和iOS,而没有特定于平台的库。
通过SQL查询提供平面表访问。即不会被迫使用语言集成的ORM之类的功能。