无法在.netstandard库项目中打开数据库(带有nhibernate的sqlite)

问题描述 投票:0回答:1

我尝试从xamarin表单项目(uwp和android)使用的.netstandard库项目中打开一个sqlite数据库我只尝试了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();
        }

    }
}

通过使用实体框架核心解决了问题

c# sqlite nhibernate xamarin.forms .net-standard-2.0
1个回答
0
投票

您可以尝试.NET Standard包装的.NET Standard实现,有两点不同:

  1. 提供跨三个主要平台的.NET标准库:UWP,Android和iOS,没有特定于平台的库。
  2. 使用SQL查询提供平面表访问。即没有被迫使用语言集成的ORM功能。

https://github.com/MelbourneDeveloper/SQLite.Net.Standard

© www.soinside.com 2019 - 2024. All rights reserved.