0

I'm trying to create a SQLite file setting the permission full for everyone. I've tried the following:

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));
    }

The file is created with full permission, however right after its creation, SQLite overrides it and the permission rules are lost.

How can I create the database file with permission rules set?

1 Answer 1

1

It doesn't look like you can when using SqliteDropCreateDatabaseWhenModelChanges. In the source code for this project, it deletes and creates a new file for the SQLite database. You may have to report an issue with that project to have it set permissions on the new database to the deleted database's permissions. As a workaround, it looks like you can use a different database creation option SqliteCreateDatabaseIfNotExists or by setting the permissions again after Database.SetInitializer has run.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.