0

If created a website using MVC3 and have a database attached to it. They are all linked together. I have a table for countries, table for states, table for cities. Now when I delete for example a country, the states and cities attached to it still remain in the database. I cant see them in the controllers list, but they do show up in the dropdown when choosing for example a state when creating a city. How do I fix this ?

thanks in advanced.

2
  • 3
    This is a very vague question. Please share your code and database model. Are there foreign key constraints between Countries, Cities, and States? If so, you may be able to use ON DELETE CASCADE for the foreign keys. Commented Mar 19, 2012 at 17:09
  • THis is explained in asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/… Commented Mar 19, 2012 at 19:18

1 Answer 1

3

Use foreign key relationships set them to ON DELETE CASCADE as shown in the screenshot. That way when you delete a country, it will automatically delete the states, cities, etc that depend on it.

enter image description here

Or manually loop through and delete them yourself starting with cities, then their states and then the parent country.

You should be using foreign keys though..

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I was using foreign keys but I think I dont have that option selected!
+1 more. I have been using code first so long I never realized mssql finally got SetNull cascade option. I know it's been in ANSI sql for a while, and have always wondered when it would come to sql server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.