Question

I have a seeder for my Entity framework, a function ResetTables(), which deletes all rows from all tables, then reseeds them all.

Foreign key relationships are blocking my delete statements, I managed to get around the issue for awhile by deleting things in a precise order but now, even that's not working.

How can I get around this.

Here's an example of my ResetTables() method

db.Database.ExecuteSqlCommand("INSERT INTO Badges (Name) VALUES (null)");
db.Database.ExecuteSqlCommand("DELETE FROM Badges; DBCC CHECKIDENT(Badges, RESEED, 0)");

NB: The insert and reseet is just so things get set back to 0 so that my IDs don't climb to high.

Was it helpful?

Solution

If you want to clear database and you don't want to deal with constraints you can temporarily turn them off, execute your deletion and turn on constraints.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top