我使用 SQLite 数据库使用 .NET Core 2.2 创建了一个 ASP.NET MVC 网站。到目前为止,它运行良好。当我想将特定于 SQLite 的关键字添加到连接字符串时,问题就开始了,例如
Data Source=~\\App_Data\\MyDb.db; Version=3; DateTimeFormat=UnixEpoch; DateTimeKind=Utc
Run Code Online (Sandbox Code Playgroud)
现在我得到
不支持关键字:'版本'
我像这样注册数据库
// ConfigureServices(IServiceCollection services)
var conn = Configuration.GetConnectionString("MyDB").Replace("~", _env.ContentRootPath);
services.AddDbContext<MyDBContext>(options => options.UseSqlite(conn));
Run Code Online (Sandbox Code Playgroud)
然后 MyDBContext 有
public partial class MyDBContext : DbContext
{
public MyDBContext() { }
public SatrimonoContext(DbContextOptions<MyDBContext> options)
: base(options) { }
public virtual DbSet<Book> Book { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我在我的页面模型中使用它
private SatrimonoContext _db;
public BookAccuracyListModel(SatrimonoContext dbContext)
{
_db = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
Run Code Online (Sandbox Code Playgroud)
从那里我可以通过 LINQ 正常访问 …
我有一个Xamarin表单应用程序,它创建一个Sqlite数据库.
Microsoft.EntityFrameworkCore.Sqlite
用于创建数据库.我想为文件添加密码.我搜索了互联网,但不幸的是我找不到任何明显的方法.在StackOverflow上,有一些问题与我的问题类似,但是,该平台不是Xamarin Forms.
以下是问题:
这是我创建数据库的代码:
public class DoctorDatabaseContext : DbContext
{
private readonly string DatabasePath;
public virtual DbSet<OperationsNames> OperationsNames { get; set; }
public virtual DbSet<CommonChiefComplaint> CommonChiefComplaint { get; set; }
public virtual DbSet<CommonDiagnosis> CommonDiagnosis { get; set; }
public virtual DbSet<CommonLabTests> CommonLabTests { get; set; }
public DoctorDatabaseContext(string DatabasePath)
{
FixedDatabasePath.Path = this.DatabasePath = DatabasePath;
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Filename={DatabasePath}");
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用EntityFramework Core 1.0(EntityFramework 7的新名称)来管理C#Desktop应用程序中的SQLite数据库。
我阅读了官方文档,并且我的测试应用程序正常运行。
我的问题是:可以用密码保护数据库吗?
我知道有很多方法可以做到,它也存在允许执行此操作的.NET类System.Data.SQLite,但是如何使用EFCore 1做到这一点?
我不知道是否有可能。
感谢帮助,
恩里科
我使用SQLite和Entity Framework.
我使用以下代码创建数据库:
class MyContext : DbContext
{
// DbSets, OnModelCreating(), etc.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=c:\\test.db");
}
}
//this is in my test method
using (var db = new MyContext())
{
db.Database.EnsureCreated();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码有效.数据库被创建.但我想通过在连接字符串中提供密码来加密数据库:
optionsBuilder.UseSqlite("Data Source=c:\\test.db;Password=mypassword");
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在EnsureCreated
调用之后,我得到了异常
System.Data.dll中发生未处理的"System.ArgumentException"类型异常附加信息:不支持关键字:"password".
使用密码有什么问题?如何加密SQLite数据库?
对于这个应用程序,我有一个使用sqlcipher的数据库,该密码保护.
我有一个活动,允许我使用以下代码更改密码:
SQLiteDatabase database = SQLiteDatabase.openDatabase(getDatabasePath("db").getPath() , oldPass, null,0) ;
database.rawExecSQL("PRAGMA rekey = '"+newPass+"';");
database.close();
Run Code Online (Sandbox Code Playgroud)
执行前一段代码后,应用程序恢复正常操作,同时尝试再次访问数据库,我得到:
10-21 16:40:28.961 26635-26635/com.example E/Database: CREATE TABLE android_metadata failed
10-21 16:40:28.981 26635-26635/com.example E/Database: Failed to setLocale() when constructing, closing the database
net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2101)
at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1967)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:900)
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:943)
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
Run Code Online (Sandbox Code Playgroud)
其次是:
Caused by: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2101)
at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1967)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:900)
at …
Run Code Online (Sandbox Code Playgroud)