0
我試圖創建一個SQLite文件,爲每個人設置權限。我已經試過如下:使用SQLiteConnection創建具有完整權限的SQLite數據庫文件
class StudioContext : DbContext
{
public IDbSet<PinnedCommand> Pinned { get; set; }
public StudioContext()
: this(Constants.StudioDataFilePath)
{
}
private StudioContext(string databasePath)
: base(GetConnection(databasePath), true)
{
Configuration.ProxyCreationEnabled = false;
}
private static DbConnection GetConnection(string databasePath)
{
var dir = new DirectoryInfo(Path.GetDirectoryName(databasePath));
if (!dir.Exists)
dir.Create();
GrantFullPermission(databasePath);
var connSb = new SQLiteConnectionStringBuilder()
{
DataSource = databasePath,
ForeignKeys = true,
BinaryGUID = true,
DateTimeFormat = SQLiteDateFormats.ISO8601,
DateTimeKind = DateTimeKind.Utc
};
return new SQLiteConnection(connSb.ConnectionString);
}
private static void GrantFullPermission(string databasePath)
{
if (File.Exists(databasePath))
return;
var rule = new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, AccessControlType.Allow);
var security = new FileSecurity();
security.AddAccessRule(rule);
using (File.Create(databasePath, 100, FileOptions.None, security))
{
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
Database.SetInitializer(new SqliteDropCreateDatabaseWhenModelChanges<StudioContext>(modelBuilder));
}
該文件是用完全權限創建,但在創建後,SQLite的覆蓋它和權限規則都將丟失。
如何創建設置了權限規則的數據庫文件?