Entity framework LiteDB的实体框架数据提供程序

Entity framework LiteDB的实体框架数据提供程序,entity-framework,litedb,Entity Framework,Litedb,我希望我的EntityFrameworkDBContext类能够处理不同的-2数据库。它可以使用各自的数据提供程序与SQLServer、MySql和SQLite配合使用。但是无法获得LiteDB的任何数据提供程序(没有sql)。是否有关于LiteDB实体框架的文章或示例代码。您的问题非常相关。LiteDB的API与EntityFramework非常相似。在我看来,我认为没有必要将EF与LiteDB一起使用。但那只是我的想法。回答您的问题,仍然没有实现兼容性。附件是问题链接 您是否计划支持Enti

我希望我的EntityFrameworkDBContext类能够处理不同的-2数据库。它可以使用各自的数据提供程序与SQLServer、MySql和SQLite配合使用。但是无法获得LiteDB的任何数据提供程序(没有sql)。是否有关于LiteDB实体框架的文章或示例代码。

您的问题非常相关。LiteDB的API与EntityFramework非常相似。在我看来,我认为没有必要将EF与LiteDB一起使用。但那只是我的想法。回答您的问题,仍然没有实现兼容性。附件是问题链接

您是否计划支持EntityFramework#1198

您可以通过实现一个公共数据库接口来解决这个问题

像这样:

公共接口IDatabaseService
其中tenty:Entity,new()
{
无效插入(TEntity实体);
}
LiteDB(此类可以是一个抽象类,以确保不会忘记SQL server数据库而进行实例化):

公共类LitedService:IDatabaseService
其中tenty:Entity,new()
{
私有字符串_stringConnection;
私有LiteDatabase_db;
受保护的LiteCollection\u collection;
公共数据库服务()
{
_stringConnection=string.Format(“filename={0};journal=false”,DependencyService.Get().GetLocalFilePath(“LiteDB.db”);
_db=新的LiteDatabase(_stringConnection);
_collection=_db.GetCollection();
}
公共无效插入(TEntity实体)
{
_收款。插入(实体);
}
}
SqlServer:

公共类SQLServerService:LiteDBService、IDatabaseService
其中tenty:Entity,new()
{
私有只读MyContext\u上下文;
公共SQLServerService(MyContext上下文)
{
_上下文=上下文;
}
公共无效插入(TEntity实体)
{
_context.Set.Add(实体);
_SaveChanges();
基础。插入(实体);
}
}
public interface IDatabaseService<TEntity>
        where TEntity : Entity, new()
{
    void Insert(TEntity entity);
}
public class LiteDBService<TEntity> : IDatabaseService<TEntity>
        where TEntity : Entity, new()
{
    private string _stringConnection;
    private LiteDatabase _db;
    protected LiteCollection<TEntity> _collection;

    public DatabaseService()
    {
        _stringConnection = string.Format("filename={0};journal=false", DependencyService.Get<IFileService>().GetLocalFilePath("LiteDB.db"));

        _db = new LiteDatabase(_stringConnection);
        _collection = _db.GetCollection<TEntity>();
    }

    public void Insert(TEntity entity)
    {
        _collection.Insert(entity);
    }
}
public class SQLServerService <TEntity> : LiteDBService<TEntity>, IDatabaseService<TEntity>
            where TEntity : Entity, new()
{
    private readonly MyContext _context;

    public SQLServerService(MyContext context)
    {
        _context = context;
    }

    public void Insert(TEntity entity)
    {
        _context.Set<TEntity>.Add(entity);
        _context.SaveChanges();

        base.Insert(entity);
    }
}