ADO.NET command to script database creation - c#

Is there a SELECT command I can execute in ADO.NET to retrieve a script that will rebuild my database from scratch with empty tables? I'd like to be able to script the creation of the database itself, along with all the tables, views, and stored procedures and any relationships that exist between the tables. Also primary key and identity specifications.
I am using SQL Server 2000 and C# (.NET Framework 2.0).

There is no such SELECT command but you can use SQL Server Management Studio to generate such a script for you.
Context menu for a database in Object Explorer has Script Database as option that should do the trick for you. Then you could execute the resulting script from your program using standard SqlCommand objects.

Depending on what you are trying to do, you may consider to build your database structure into the model (System databases / model). Then, each time you'll create a new database, you'll get all the stuff you need already inserted into your new database.
But be warned that this solution will create all the stuff for ALL the new databases created.

Related

Using EF Core exclusively for managing stored procedure migrations

I am currently looking at a way to automate the deployment of stored procedure changes as we are running the stored procedure SQL manually on all environments for each release.
The first solution I thought of was using EF Core migrations. Is this the most suitable option or is there a better way?
Some things to consider:
The database related to this repository I am working on is synced with the main database, so there is basically no database management
It does not have any models of its own
The repository is for data analytics and reporting, so it will mostly be read-only
The only items that will change regularly are the Telerik report files and corresponding stored procedures
You didn't say which database you're using, but if it's any variety of Microsoft SQL Server I find SQL Server Data Tools for Visual Studio a good solution for maintaining all database objects, including stored procedures. Deployment to as many targets as you want is straightforward, and based on the tool comparing the current db objects to your project objects.
Visual Studio SSDT

c# code first DB migration with stored proc versioning control

Our project is using EF code first approach, and it has quite a few stored proc.
Currently we generate DbMigration code like this
var scripts = StoredProcedureMigrationHelper.GetSqlBatchFromEmbeddedResource("myStoredProc.sql");
foreach (var script in scripts)
{
Sql(script);
}
The problem with this is that every time when I update the stored proc, I have to create new sql file, with name convention like "myStroedProc_versionX". Which is working but lose the version control benefit such as show the difference between versions.
Is there a way / different approach to update stored proc with version control in code first?
A more robust and flexible approach could be to use EF database first and to model your database using Sql Server Data Tools (SSDT). By creating Database project in your solution, you could build a dacpac and apply it on SQL Server instance to update your schema to the desired state.
Disclaimer: I'm a product manager at Redgate Software, makers of ReadyRoll
If you have Visual Studio 2017 Enterprise, which ships with ReadyRoll Core edition, you can switch to using SQL-based migrations for your database deployments. This would allow you to include the deployment of your stored procedures alongside your schema changes, while still allowing you to use EF CodeFirst to do your modelling.
You can read more about this approach in the ReadyRoll documentation:
https://documentation.red-gate.com/display/RR1/Tutorial%3A+Entity+Framework+CodeFirst+migrations
Note that the article makes use of the programmable objects feature of ReadyRoll, which is only included in the Pro edition of ReadyRoll. As an alternative, you could script your stored procedures as post-deployment scripts (although this will cause the scripts to run with every deployment, rather than just on each change).
Unfortunately, EF doesn't provide a native solution for that, but I found a solution and I explain it in this post: https://softcadbury.github.io/dotnet/entity-framework/2022/05/10/versionize-stored-procedures-with-entity-framework.html
The idea is to embark the SQL code of your SQL stored procedures in your solution (works also with SQL views or SQL functions) and with a few C# extensions, you can manage them in EF migrations.

How to reflect local database changes to TEST environment database in entityframework database first

I have a MVC4 + EntityFramework Database First application. I have made some changes in my local database (added table and columns in couple of tables). After this I updated my .edmx file and ran the custom tool. This has updated my models of the table whose schema i have changed. Everything is working fine.
I want to know, How to reflect those local database changes on my Test database?
Within VS you can compare the two databases and generate a change script that will bring them on par with each other.
In your case you want to do a schema compare.
Tools -> SQL Server -> New Schema Comparison.
You select your local database and then select the test database. it will compare the schema of both and show you the differences. You can select which you want to apply and either apply it directly or generate a change script and execute when you want from SSMS.
Compare and Synchronize Database Schemas
In SQL Server Management Studio you can export the database script to drop and recreate the tables, indexes, relationships, etc... You can also use Visual Studio to export the SQL script to create all the tables. The other way is to manually add the updated columns. I'm unsure of any other ways to do this in a database first approach.
Try SQL Server Data Tools for Visual studio :
https://msdn.microsoft.com/en-us/mt186501.aspx
Very easy and intuitive to use for Schema comparison and generating delta scripts.
I suggest you have a look at xSQL Schema Compare. It's a great tool for comparing and synchronizing database schemas. It should do the trick in your case. Also, this comes bundled with xSQL Data Compare which you can use to compare and synchronize the data as well, should the need to do so arise.

Entity Framework & SQL Server 2008 Express

I'm totally new to Entity Framework and have done some reading, and as a test I have put together a very brief test framework with just two small Entities. I then right clicked and selected "Generate Database from Model" which takes me to the SQL Connections page. However, none of the previous connections I have used appear in the drop down list, and when I select Create New Connection I only have the options to use 'SQL Server Compact 3.5' or 'SQL Server Database File'.
I have come across this before with SQL Express and the work around is to create my own Connection String to access the required Database. However, with me using Entity Framework to create the database, it is impossible to write an appropraite connetion string.
I therefore seem to be in a Catch 22 situation.
I cannot write a connection string until the database is created.
I cannot create the database from EF without accessing SQL Server (via a connection string).
Anyone come across this or can point out what I'm doing wrong. Like I said I'm totally new to EF so I apologise if this is a very basic question.
Unless you want to use a database file in a user instance, you need to either use SQL Management Studio Express, or use the SQLCMD command line tool to create the database. I would recommend SQL Management Studio Express as it is easy to learn in my opinion.
The Entity Framework tools are intended to be used to create the database schema, not the database itself. You still need to define the file groups, security information, and other basics of creating and configuring a blank database.
See this Q/A for appropriate links:
How to create DB in SQL Express using SQL commands?
Thanks #psuedocoder, your assistance helped me resolve the problem. The answer in the end was simple, but not intuitive to me, hence my difficulty. I thought some elaboration on the answer might help others who are equally new to Entity Framework as me.
From Visual Studio
Create your EF model in Visual Studio.
Right click the model canvas and select 'Generate Database from Model'.
You are then asked to select the database connection. Select 'New Connection'.
In the 'Add Connection' dialog box, rather than 'Browse' for an existing database, just simply type the name of your new database in the Database text box and Click Connect.
When you try to connect you will get a warning saying the database does not exist, but you will be asked if you want VS to 'Create It'. Select Yes.
As #psuedocoder states this does not actually create the database in SQL Server, but it does create an object in your VS soluton which contains the TSQL script required in order to create your database.
From SSMS
Go to SQL Server Management Studio and create a blank database of the same name used in step 4 above.
In SSMS select open file and navigate to the windows folder containing your VS solution files.
Open the TSQL script file. This will have a DB Script icon and have an .edmx extension.
Click Execute, and you will have your new database created from your EF model.

Create a table in an existing DB with LINQ to SQL

I have a SQL Express 2008 DB that has gone live. I'd like to create a new table in that database without deleting all the existing data in the live DB.
I created an entity class for the new table and a separate data-context.
What is the best way for me to add the new table using LINQ to SQL
Thanks...
That's not Linq To Sql's role in your architecture. You need to create the tables prior to deploying your new version of your data model.
You can either manually update your db w/ direct SQL commands against it, or you can package it w/ the deployment (e.g. via continuous integration scripts or something like Migrator.NET)
In the new Linq to Entity which shipped with Visual Studio 2010 there is easy way to create Database table from Model you built.
here is the Example : Create a Database using Model-First
Why not just execute the SQL statement, like create table [mytable] ...?
Why you add unmapped-entities to diagram? I think, first you have to create tables after map entities.

Categories

Resources