VS2022 publish a new database from database project.

Josef Szeliga 20 Reputation points
2023-06-28T00:57:50.8766667+00:00

I'm not sure if this is a bug or if I am using the "Publish" option incorrectly.

I have created a SQL database project in VS2022. This project will create a new SQL Database called "BuzzAlert".

The issue occurs when I use the Publish option. The Publish Database dialog screen requires a connection string and the name of the database.

The problem occurs when I enter the name "BuzzAlert" into the Database Name text box.

It updated the connection string with that database name. As the script is meant to create the database It cannot connect to the "BuzAlert" database which as yet does not exist.

This causes VS to hang. The "Database Name" text box is mandatory.

Is there a way around this? Should I be able to use the "Publish" option to create a new database?

Any help appreciated.

Developer technologies Visual Studio Other
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 34,436 Reputation points Microsoft External Staff
    2023-06-28T09:53:14.6033333+00:00

    Hello @Josef Szeliga ,

    Welcome to Microsoft Q&A forum.

    As far as I know, "publish" is more like deploying an existing database from Visual Studio to one of the existing databases of SQL Server. So, you may get errors and fail to publish.

    If you want to create a new database, you may check and follow this document: Create a database and add tables in Visual Studio.

    Best Regards,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Josef Szeliga 20 Reputation points
    2023-06-28T10:04:22.4266667+00:00

    Thank you. Does that mean that while I'm developing, I should use the process described here Create a database and add tables in Visual Studio. ? I used the Database project "schema compare" to help me design my database objects and scripts. I spent about a week trying get the publishing script to delete and create the database from scratch. In the end i had to put so much in the pre and post deployment scripts for it to work that I could have written the published script by hand and saved time.

    I'll give your suggestion I try. It can't be worse.

    Josef

    0 comments No comments

  3. Josef Szeliga 20 Reputation points
    2023-06-29T05:20:18.5+00:00

    If anyone else finds this question, whatever you do, do not go to Create a database and add tables in Visual Studio. ?

    It's not a dead end, but a stupid dead end.

    My aim was to build a script which I run when needed to drop and recreate or modify my database as I develop it.

    The database was to be located on a local SQL server.

    The Create a database and add tables in Visual Studio. gets you to create a .NET Framework Windows Forms projects. Then adds an .mdf file (this will be your database) to it. You then build your tables etc via the visual studio interface. And this is where the documentation ends. There is no way to publish this to SQL. I guess the form is there for later to be run which is never explained. There are gaps in how you script things like schema. Each time you generate a table with a concatenated key the build hangs.

    If you are in high school or 1st year in university. This might be how you would start a small project for class. But it is so limited and flaky it no good for anything else.

    0 comments No comments

  4. Josef Szeliga 20 Reputation points
    2023-06-29T23:19:08.7266667+00:00

    I didn't find an answer. The Visual studio SQL database project can't handle dropping and creating a database out of the box. You will ned to create your own script to drop and create the database first and then run the publishing script. So in essence SQL database projects are not fit for purpose.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.