由于我没有得到满意的答案,我将张贴我最终做了什么。我认为这可能是一种不错的方式,但可能还有其他更好的方式来实现我所寻求的(在我的数据库中使用LINQ类型语法,而不是使用包含查询的字符串)。
TL;DR:使用SQLite。CodeFirst+EntityFramework。
首先,您需要添加以下软件包:
-
-
系统数据SQLite(可能也会安装:)
-
系统数据SQLite。果心
-
-
系统数据SQLite。林克
最后,如果你从代码开始(就像我一样),你还需要
接下来要做的是在应用程序中设置连接字符串。配置。
(另一方面,在指定提供程序时似乎有很多错误,卸载和重新安装上述软件包似乎可以修复这些错误。我不太确定删除和添加以及不变名称背后的逻辑-如果你关心我写的内容,那么是这样的:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
</providers>
</entityFramework>
)
连接字符串应该指定一个名称,以及至少一个DB文件所在的路径。您还可以使用稍后在代码中定义的相对路径(通过使用| DataDirectory |语法):
<connectionStrings>
<add name="YourModel" connectionString="Data Source=|DataDirectory|\NameOfYourDBFile.sqlite" providerName="System.Data.SQLite" />
</connectionStrings>
下一步,如果您先编写代码,则创建一个新类作为您的模型,这基本上是使用SQLite。CodeFirst软件包:
class YourModel : DbContext
{
// Your context has been configured to use a 'YourModel' connection string from your application's
// configuration file (App.config or Web.config). By default, this connection string targets the
// 'YourProject.YourModel' database on your LocalDb instance.
//
// If you wish to target a different database and/or database provider, modify the 'YourModel'
// connection string in the application configuration file.
public YourModel()
: base("name=YourModel")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<YourModel>(modelBuilder);
Database.SetInitializer(sqliteConnectionInitializer);
Database.SetInitializer(new SqliteDropCreateDatabaseWhenModelChanges<YourModel>(modelBuilder));
}
// Add a DbSet for each entity type that you want to include in your model. For more information
// on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.
public virtual DbSet<YourTableClass> YourTable { get; set; }
}
[Table("YourTable")]
public class YourTableClass
{
[Key]
public string Id { get; set; }
[Required]
public FileOperation Oper { get; set; }
[Required, Index]
public OperationState State { get; set; }
[Index]
public string Proc { get; set; }
[Required]
public string Src { get; set; }
public DateTime Timestamp { get; set; }
// etc.
}
你可以了解更多
here
AppDomain.CurrentDomain.SetData("DataDirectory", @"the\path\you\desire");
现在你可以用它了。基本语法非常简单,您只需使用:
using (var context = new YourModel())
{
// some query
}
选择
using (var context = new YourModel())
{
var t = context.YourTable
.Where(e => e.State == OperationState.BackedUp)
.Select(e => e.Proc)
.Distinct()
}
如果你想的话
插入
using (var context = new YourModel())
{
var e = context.YourTable.Create();
e.Id = guid;
// ...etc
e.Timestamp = timestamp;
context.YourTable.Add(e);
context.SaveChanges();
}
如果仍要使用字符串查询:
using (var context = new YourModel())
{
context.Database.ExecuteSqlCommand(comString);
}
-
如果您更改数据库中的某些内容,您必须致电
Context.SaveChange()
最后(ExecuteSqlCommand不需要这个)
-
删除可以通过RemoveRange+SaveChange()或仍然使用查询字符串来完成。
public static IQueryable<YourTable> GetFilteredQueryable(IQueryable<YourTable> yourTable)
{
// filter by time
switch (RestoreLogic.WithinTimeBtnStatus)
{
case WithinTime.All:
break;
case WithinTime.Hour:
DateTime offsetHour = DateTime.Now.Add(new TimeSpan(-1, 0, 0));
yourTable = yourTable.Where(e => e.Timestamp >= offsetHour);
break;
// etc.
}
// filter by extension
foreach (var i in FilteredExt)
{
yourTable = yourTable.Where(e => e.Ext != i);
}
// etc.
return yourTable;
}