1

I want to create a cascading dropd own list. For that I created 3 model classes State,District and User. But while running an exception occured.

public class State
    {
        public int StateID { get; set; }
        public string StateName { get; set; }
    }

 public class District
    {
        public int DistrictID { get; set; }
        public string DistrictName { get; set; }

        public int StateID { get; set; }

        public virtual State state { get; set; }
        }
 public class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }

        public int StateID { get; set; }
        public int DistrictID { get; set; }

        public virtual State state { get; set; }
        public virtual District district { get; set; }

    } 

The exception is,

"An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Introducing FOREIGN KEY constraint 'FK_dbo.Users_dbo.States_StateID' on table 'Users' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Could not create constraint. See previous errors."

Can anyone help me to solve this problem?

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
  • 1
    You should edit the title to make it more specific. Also check: http://stackoverflow.com/questions/14489676/entity-framework-how-to-solve-foreign-key-constraint-may-cause-cycles-or-multi or http://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths – blfuentes Jun 23 '15 at 07:00
  • Remove the `public int StateID { get; set; }` and `public virtual State state { get; set; }` properties from `User` –  Jun 23 '15 at 07:08
  • ok. but then how can I create drop down for state in User? –  Jun 23 '15 at 07:19
  • Does this happen during migration/database creation? You could change the mapping of entities (to avoid the FOREIGN KEY error) as suggested. – bubi Jun 23 '15 at 08:22

2 Answers2

0

This happens when your foreign key relations are defined as ON DELETE CASCADE. To override it in your fluent api relations definitione add .WillCascadeOnDelete(false):

HasRequired(e => e.State )
   .WithMany()
   .HasForeignKey(e => e.StateID)
   .WillCascadeOnDelete(false);

You have to find out for which relationships you need to alter this delete behaviour. From my experience it is generally better in almost all cases to have CascadeOnDelete set to false as it leads to more predictable behaviour of db. ON DELETE CASCADE may lead to some unexpected deletes. It is only helpful for tables serving as join table for many-to-many relationships.

mr100
  • 4,340
  • 2
  • 26
  • 38
0

My friend Subin Jacob(user:1923685) helped me to solve this problem.

In UserModel Class write the following code

public class ProjectContext : DbContext
    {
        public DbSet<State> state { get; set; }
        public DbSet<District> district { get; set; }
        public DbSet<User> user { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention();
        }
    }

and in Global.asax.cs

    Database.SetInitializer<ProjectContext>(null);