Question

I'm trying to set the initial database file size and its auto growth parameter in a Database Project in Visual Studio to be used with the Publish option. I've added a Filegroup file (SqlFile1.sql) to the project with the following contents:

ALTER DATABASE [$(DatabaseName)]
    ADD FILE
    (
        NAME = [SqlFile1],
        FILENAME = '$(DefaultDataPath)$(DefaultFilePrefix)_SqlFile1.ndf',
        SIZE = 5000MB,
        MAXSIZE = UNLIMITED,
        FILEGROWTH = 50%
    ) TO FILEGROUP [PRIMARY]

(documentation).

When I click Publish with the option Always re-create database checked, the file SqlFile1 is created within the PRIMARY filegroup, but has the Initial size set to 3 MB and Autogrowth is By 1 MB, unrestricted growth. There are no error or warning messages.

What's interesting, the following SQL script:

ALTER DATABASE DatabaseTest
MODIFY FILE (NAME = [SqlFile1], SIZE = 5000MB, FILEGROWTH = 50%);
GO

ran from SQL Management Studio correctly modifies file's properties.

Why are my settings ignored and how to make them work?

Was it helpful?

Solution

The scripts created in the bin directory suggest that properties other than logical file name, physical file name and target file group are indeed ignored. Eventually I solved this issue by adding a post-deployment script. To do this right click the database project and choose Add -> New item -> User scripts -> Post-Deployment Script. In this script you can modify the database anyway you want, in this case alter file's properties with:

ALTER DATABASE [$(DatabaseName)]
    MODIFY FILE
    (
        NAME = [SqlFile1],
        SIZE = 5000MB,
        MAXSIZE = UNLIMITED,
        FILEGROWTH = 50%
    )

OTHER TIPS

The correct size and filegrowth settings can be set by creating a Filegroup file object the way you've been trying, but the key is that you also need to set an advanced publish setting of Script file size to true.

enter image description here

Now there's another gotcha to this, in that if you publish/create the database first without this option checked, then subsequent publish operations will not correctly apply the file size settings even if you've since checked this option. This is likely because the object has already been created, though I don't see why the dacfx libraries couldn't perform a similar diff to the way they do on tables. At this point you'd need to either manually change the file settings or add a post deployment script as you've mentioned here.

For reference here is a comparison to show the difference in initial create database operations with and without the Script file size option.

Without Script file size enter image description here

With Script file size enter image description here

With the option checked you can see the correct file size options are applied to the database deployment without the requirement of a post deployment modify db script enter image description here

According to this post of a user who had the same issue, these setting are apparently missing in database projects:

only some database properties are available in VS database projects. In particular size and autogrowth settings are missing.

This blog however does suggest you can work around it by editing values in the .sql files directly (not via the IDE):

You can change filegrowth (and other) settings of the database project, however these aren't available through the VS 2010 IDE. Instead you have to edit .sql files found here:

[Project location][Database project name]\Schema Objects\Storage\Files

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top