How do I delete all data in the Seed method? - entity-framework

I'm using Entity Framework code first with migrations and I want to delete all data in the Seed method, from all tables, except from the __MigrationHistory table. Right after my database is clean, I'll proceed with the seed method, but just adding new objects.
I want this because I develop using a development database and I constantly want to reset it to a default state.
So, what's the best way to delete all data, from all tables, except for __MigrationHistory, in the seed method?

public override void Seed(YourNamespace.Model.DbContext context)
{
// Deletes all data, from all tables, except for __MigrationHistory
context.Database.ExecuteSqlCommand("sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'");
context.Database.ExecuteSqlCommand("sp_MSForEachTable 'IF OBJECT_ID(''?'') NOT IN (ISNULL(OBJECT_ID(''[dbo].[__MigrationHistory]''),0)) DELETE FROM ?'");
context.Database.ExecuteSqlCommand("EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'");
// proceed with the seed here
}

Related

Postgres Select FOR UPDATE NOWAIT

I have a table called account, and I'm selecting a record from this table and I want it to be locked so I'm using UPDATE NOWAIT.
the problem is when I try to insert a record in another table (transaction) which has a foreign key to this table it takes for ever and so I know that it's somehow related to the lock.
Is it the desired functionality ?
Because I'm inserting a record to another table. and the other query might update that record but will not delete it so why not be able to insert a relation?
and what is the solution?

How to delete all the triggers and procedures from a single table?

I am working on a problem where I need to rename the table name. The table has few triggers and procedures attach to it.
My steps to do that:
Delete all the triggers and procedures of the table name employee.
Rename table name(employee) to user using ALTER command.
Then create all the triggers and procedures according to the new table.
Database I am using is postgresql.
Below query is used to delete the single trigger on a table.
DROP TRIGGER IF EXISTS update_employee_changes on employee;
But I want to delete all the triggers and procedures related to the given table in a single query.
You don't have to drop the triggers and functions. Just use a transaction:
BEGIN;
ALTER TABLE ... RENAME TO ...;
CREATE OR REPLACE FUNCTION /* function that uses the new name */
...
COMMIT;
You only have to replace the trigger functions and other functions. Triggers, views etc. don't have to be modified.

Phinx changing migration doesnt create table again

I had a migration that created a table with 2 columns. Ran the migration and it worked. But I realised I would like one of the columns to be a different data type. I dropped the table in my db, changed the migration and ran it again and the table isn't created ? I tried using change() and create()
you need to create a new migration to change columns, don't do that in the old migration, because when you run a migration a new row will be inserted in phinxlog table, and if you try migrate again , phinx will check on phinxlog table what migration has been executed, so deleting table is not enough, you need to delete the inserted row in phinxlog. but it will be good if you create a new migration.

Entity Framework Code First Migration and Data Migrations

We are using Entity Framework Code First with Migrations. We have it running without any issues. We are trying to migrate data from an old database to the new. We need to drop the database, create all the tables, insert the data and then add in the primary keys and foreign keys. We want to do this in EF so that we can format the data being migrated. We have been successful in creating the database and then migrating the data over but the primary keys are in the old database are not coming over. I've tried using the ExecuteSqlCommand before a bulkinsert:
ctx.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[TableName] ON");
but this does not work because the migration scipts have the idenity set to true:
Id = c.Int(nullable: false, identity: true),
Is there a way to set the identity to false then after the data is inserted into the database, set the identity to true?
You are doing it in the right way (Id is an identity and you enable identity insert). SET IDENTITY_INSERT ON is valid only for the session (the connection) so you need to use only one connection.
Also, you have to seed the database using INSERT statements (and not EF SaveChanges).

how to create a EF data model like a sql server view

I need to create a model of union of several sql server tables and i have to get ability of
insert , select , update and delete ...
(id like to use the model as same as any other model)
any suggestions ?
thanks for reading.
Edit: i tried sql server view but got the fallowing error when i want to insert to sql server view:
Msg 4406, Level 16, State 1, Line 1
Update or insert of view or function 'viewName' failed because it contains a derived or constant field.
You need to create database view + stored procedures for insert, update and delete. You will map the view as a new entity and map imported stored procedures to insert, update and delete operations for that entity.
You actually don't need the database view - you can write the query directly to EDMX by using DefiningQuery but it requires manual modification of EDMX. Default EF tools will delete your manual modification once you run Update from database again.
Even with defining query you still need those stored procedures. There is no other way to make entity based on defining query (view is also imported as defining query) updatable.