2017-08-04 66 views
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的覆蓋它和權限規則都將丟失。

如何創建設置了權限規則的數據庫文件?

回答

1

使用SqliteDropCreateDatabaseWhenModelChanges時看起來不像你可以。在該項目的source code中,它刪除併爲SQLite數據庫創建一個新文件。您可能需要報告該項目的問題,以便將新數據庫的權限設置爲已刪除的數據庫的權限。作爲一種解決方法,看起來您可以使用不同的數據庫創建選項SqliteCreateDatabaseIfNotExists或在Database.SetInitializer運行後再次設置權限。

相關問題