我正在尝试创建一个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会覆盖它,并且权限规则将丢失。
如何创建设置了权限规则的数据库文件?
答案 0 :(得分:1)
使用SqliteDropCreateDatabaseWhenModelChanges
时看起来并不像。在此项目的source code中,它删除并创建SQLite数据库的新文件。您可能必须报告该项目的问题,以使其将新数据库的权限设置为已删除数据库的权限。作为一种变通方法,看起来您可以使用不同的数据库创建选项SqliteCreateDatabaseIfNotExists
,或者在Database.SetInitializer
运行后再次设置权限。