EF Code First rollback database table design - sql

I accidentally deleted my database tables and I need to get them back. I have tried running update-database, but I only get:
Cannot find the object "dbo.ArticleComments" because it does not exist or you do not have permissions.
I also tried running Update-Database -TargetMigration:"name_of_migration" with the migration name but resulted in:
Cannot find the object "dbo.ArticleComments" because it does not exist or you do not have permissions.
I need to know how to get my database tables back with their columns (empty or not I don't care)

This may be the issue on your situation.
check about this problematic table dbo.ArticleComments.If you renamed or deleted it,then it'll give above kind of error.B'cos when you created the old migration script that was there.Now it's not there.When you try to run the same old migration script, now that table is not on your DbSet or having with different name.
Solution :
If that is the case,then you have to manually edit your migration file to reflect the current table changes.

Related

Liquibase validation failed after modifying the entity

I wanted to change the data type of one field from string to date. So i dropped the table in db. Then modified the liquibase file and ran the application. now it complains with the following message.
liquibase.exception.ValidationFailedException: Validation Failed:
So after that I reverted the liquibase file changes and ran the application. This time no error but it is not creating the table.
Please help me how to solve this issue.
I assume the failed validation was an error about checksums. This happens when you modify a changeset which was already executed and try to execute it again.
Liquibase keeps all executed changesets in a table called databasechangelog, so it can find out which changesets can be skipped during execution.
To execute a changeset again, delete the corresponding from this table before, and run Liquibase again.
When using Liquibase, you shouldn't (in general) modify the database outside of Liquibase - the main exception being if you are a developer working on your own private development database. If you are in that state (working on your own private database), then when you modify the database outside of Liquibase (i.e. dropping a table) you will also need to delete the row in the DATABASECHANGELOG table that corresponds to the table create statement so that when you re-run liquibase update it will re-create the table.

Redgate migration scripts not running on deployment

I've been reading through the Redgate documentation on migration scripts and I'm trying to add a new column to a table that has a foreign key from another table.
Here's what I have done:
Added the new column, made it null-able and created the
relationship to a new table then I've committed the changes.
I then add static data to the new table so that the migration can
run. I commit this static data.I then add a blank migration script,
and set all null values on the column I've created in the last
commit to be the Id of one of the records in the related table. I
then commit this change.
I then run a deployment of both commits to my testing environment where
records already exist.
The problem I'm having is that the column gets created but the script seems like its not running as the column values stay null. I've verified that the script should actually change the columns as I've attempted to run it manually and it executes successfully.
Am I doing something wrong when using these scripts? Thanks.
I was creating blank migration scripts which lead to SQL Compare to set the column as not null. You have to specifically create a migration script on the schema change that requires it or SQL Compare will override all changes.

Entity Framework Core apply migrations programmatically

I have test and live db. For test db migrations I use Add-Migration ... and Update-Database syntax in package manager console. But for live db I want to do it programmatically when app is started.
The following code didn't help me:
context.Database.Migrate();
I have error Invalid object name 'TempTenants' when I try to add record to table that doesn't exist. This is my new table.
But I have _EFMigrationsHistory table. And there are all my migrations even those that weren't applied. But I don't see new table.
I will have the same result if I manually remove table from test db and try to reproduce error.
So, context.Database.Migrate(); only create new db with all migrations, if it doesn't exist, but don't update (apply migrations) existing db.
Can I do that? And how can I resolve that?
It seems something went wrong. I removed unapplied migrations (records) from __EFMigrationsHistory table (also I needed to revert some table names, primary and foreign keys to previous state) and launched app again.
So, context.Database.Migrate(); apply migrations even for existing databases.

Execute an INSERT, UPDATE and DELETE against a SQL Server Database Snapshot

According to http://blogs.msdn.com/b/sqlcat/archive/2011/10/17/updating-a-database-snapshot.aspx I should be able to successfully execute an INSERT, UPDATE and DELETE against a Database Snapshot.
The idea is to create a view of a table before you create the snapshot, and then create the snapshot, and update the View in the snapshot.
I have tried this on my SQL Server 2014 (v12.0.2269) and I still get the error
Failed to update database "Snapshot2015_07" because the database is read-only.
The reason I am keen for this to work is that financials need to be frozen at a particular date, but need to be updated if errors are found in the snapshot.
Has anyone had success recently doing this?
I know there are alternatives like AutoAudit, but it is a lot of work to implement for 1-2 updates/deletes on a database with multiple tables with 5 million + rows
The view has to specify the database name (which is the original database name, not the snapshot database name), along with the schema and table name. Ensure the view you created specifies those three parts of the fully qualified object name.

Can't recreate deleted SSAS database

I didn't like the DatabaseID of my SSAS database, so I decide to delete/create it.
I generated a create script, and then a delete script. I changed the ID in the create script to the one I want.
I ran the delete script. It ran successfully. Refreshed and verified the database has been deleted.
Now when I run the create script, I get:
"Either the user [MyUserName] does not have access to the [MyDatabaseName] database, or the database does not exist."
Well, no &*^% it doesn't exist, I'm trying to create it.
Googling hasn't yielded any results so far. Any ideas? Do I need to do some additional clean-up somewhere before I can recreate the database?
The create script is still "pointing at" the old cube. Close that query window and create a new one.