Update Entity Framework .edmx from SQL Database project - sql-server

I have a Visual Studio solution that has a few projects in it.
One project is based around an Entity Framework model .edmx.
Another project is an SQL Database project.
I can currently 'Update Model from Database...' in the .edmx.
I can also use 'Schema Compare...' in the SQL Database project to update the database.
I would rather not have to push the changes from the SQL Database project to the database, then update the .edmx from the database.
I would like to update the .edmx from the SQL Database project directly. Is this possible?
NOTE: this may not be a great solution having both projects, but it is a legacy product that uses Entity Framework. At one time, the project was Database First, and the .edmx would follow the changes. However, we want to track our SQL schema changes with the SQL Database project. Both projects work well to solve their individual problems, but don't quite work together?

Related

Modify SQL Server database in ASP.NET Core project after published on a server

I have an ASP.NET Web API project using .NET 6, SQL Server, EF6 and code first approach. I have already published the project (deckerized) on a server. A client now asked me to add a new column to one of my tables, if I do this by the EF6 migration, it is easy, but this ended up deleting my current table (dropping) and then made a new migration and table which I can not because there is data in the database now. What would be my approach to modify the database now after I published the project on a server. Do I need to use the TSQL in the SSMS environment for any modification to made a copy of my database to another place and do all the changes and then copy the table again to the new table?
Many thanks

Keeping a SQL Server Database Project and the Actual Database (in SSMS) in sync during development

I am trying to figure out what the normal workflow should be between a SQL Server Database Project and the actual database in SQL Server (SSMS) once they are in sync. The main reason I am using one of these projects is to keep everything in Source Control.
Here are the steps I took to set things up:
Create the actual database and entire schema in SQL Server Management Studio.
Create a new SQL Server Database Project in VS2015.
Right click the project and import the actual database.
Now that the project is in sync with the actual database, how should I go about making changes going forward? Do I make them in the database project and then republish it or do I make them in the schema? Basically what I am saying is that I am trying to avoid making schema changes during development in both places.
Generally the best practice will be to make changes to the project, check that into source control, and publish the changes to your database as needed. Your project then becomes your source. You can branch it, merge it, and do whatever else is needed to do your development. You won't be republishing your database, though. You'll update your existing one when you publish your project. It will do a diff between your project and the db, then make the appropriate updates/alters to bring your database in line w/ the project.
I blogged about my experiences with SSDT here: http://schottsql.blogspot.com/search/label/SSDT, but there are some other great resources available if you look. There are definitely different ways to do things and what I chose may not fit your environment exactly.

What happens when we publish the database project through visual studio

I have been working on a project which has a database project in it and I used to publish that database when ever I made some changes to the scripts. Now that I noticed that when I publish the database project it builds first and creates a dacpac file and then it publishes after I selects the target database. I am interested in knowing what role does that dacpac file plays in publishing the sql database.
Also I have found this thing when I was trying to read about pro's and con's about dacpac. Is it really works like that?
Link
The biggest problem with DACPACs has to do with the way a data-tier application is released to push version changes from the DAC into SQL Server. This is done by creating a new database with a temporary name, generating the new objects in the database, and then moving all the data from the existing database to the new one. After all the data has been transferred and the post-release scripts run, the existing database is dropped and the new database is given the correct name.
The dacpac file is the compiled build output of the database project. It's analogous to a .dll file built from a C# class library project. All of the information you defined in your database project about your database is stored in the dacpac file, along with information about the relationships between the objects.
When a dacpac file is published, the target database is compared to the dacpac and the tool will figure out what T-SQL to execute to make the target database match the dacpac's definition.
Regarding the article, note that the Data-Tier Application Framework that shipped with SQL Server 2008 R2 was largely rewritten/replaced for SQL Server 2012, so that article, while correct regarding that very old version of the Data-Tier Application Framework, is not correct regarding the tools available today.
The DACPAC file is a Zip file contains an XML representation of your database schema. It does not contain any table data (unless you provide pre-and-post deployment scripts). More information is available here: https://www.simple-talk.com/sql/database-delivery/microsoft-and-database-lifecycle-management-(dlm)-the-dacpac/
When a DACPAC is deployed, the receiving server compares the difference between the current schema and then updates your schema accordingly by generating a change script. However, be careful, as some changes can be very expensive (such as adding a new column in the middle of a table that already has millions of rows).
The article I linked to shows you how you can view the generated change script and see what happens. Repeated here is a snippet that does it:
"%ProgramFiles(x86)%\Microsoft SQL Server"\110\DAC\bin\sqlpackage.exe
/Action:Script
/SourceFile:MyPathAndFileToTheDacPac
/TargetConnectionString:"Server=MyTargetInstance;Database=MyTargetDatabase;Integrated Security=SSPI;"
/OutPutPath:"MyPathAndFile.sql"
Using DACPACs and Database Projects (in SSDT, but do not use SQL Server Management Studio) is the preferred way of pushing database changes now as it is less error-prone than manually redesigning tables using the table designer (which will drop-recreate-and-repopulate tables if you do things like add non-terminal columns to existing tables).
I'm not too familiar with it but played around with some database uploads myself. From what I gathered the dacpac has settings that can be used and uploaded. I found these instructions:
•To create a database project based on a dacpac, create a new SQL Server Database Project in Visual Studio. Then right-click on the project in Solution Explorer and choose "Import -> Data-tier Application (*.dacpac)" and select your dacpac. That will convert the contents of the dacpac into scripts in the project, and if you choose "Import database settings" the database options will be set based on the settings in the dacpac.
Dacpac is A data-tier application (DAC) is a logical database management entity that defines all of the SQL Server objects - like tables, views, and instance objects, including logins – associated with a user’s database. A DAC is a self-contained unit of SQL Server database deployment that enables data-tier developers and database administrators to package SQL Server objects into a portable artifact called a DAC package, also known as a DACPAC. from https://msdn.microsoft.com/en-us/library/ee210546.aspx
hope this helps...

Update a SQL Server database from an Entity Framework Model

I'm using VB.net, EF6 with SQL Server 2008R2 database.
This is the situation: I have created the application. Using wizard I have created the Entity model from an existing database.
A client started using this application using this database on his computer.
After some months, I made some modifications on the database and I have updated the model on my application.
Now I have a new .exe file that has the new model from the new database.
I put the new .exe file on the client computer.
Now on his computer, the .exe file has the new database model, but the SQL Server database has the old structure.
I want to know: is possible to update the database structure from the entity model on application?
I want to add a command on application that can make (if is possible) this update, so the database become up to date according to entity model?
Thank you !
If you have applied code-first approach, then you should go for below solution , given on MSDN, so your generated script will be 'idempotent'.
Run the Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: AddPostAbstract command in Package Manager Console
after going through it and still facing issue then try below command
Update-Database -Script -SourceMigration:0
If possible, try and deploy the database as a data-tier application. It will make managing migrations and upgrades a lot easier. More info here, https://msdn.microsoft.com/en-us/library/ee210546.aspx

Database project connect to database instance - how to manage a subset of the actual database in the project

We have an existing SQL Server database, and I'd like to create a Visual Studio database project for it and put some of the scripts for the stored procedures in that database in source control. So, I thought I would create a new database project. Is there a way I can wire up this database project to the database? I thought it might be "Add Database Reference", but that only gives me options to use some other database project in the current solution, not set up the current project to be connected to an actual database.
Ideally, I'd like to be able to right-click on my project and do a "Publish" and have all my database info prefilled. I realize that I might be thinking of this wrong, but searching around on the web is of little help. It is surprising how poorly these concepts are documented.
EDIT: After the first answer, maybe my question really is: How can I have only a part of a database managed in a database project? I had assumed this was an ordinary thing that people did all the time with database projects, but maybe not. In my case, I would like to have only some of the stored procedures in source control.
After further reasearch, it seems the answer is, "no, there is no way" to have only a part of a database managed in a database project.
I'm using vs 2013 but i think this is valid back VS 2008
Right click database project.
Then choose -> import -> database.
Setup a connection to your target database.
When you import select the import setting Folder structure Schema\Object type
And run the wizard.
This builds a folder structure containing sql script for your schema objects.
Alternatively if you have sql data tools you could run the schema compare against a blank project.

Resources