3

I use this step for generating CodeFirst Classes in my MVC project:

1) run my app with membership support and call MVC Action that use membership to create membership default tables (Membership,User,UserInRoles,...)

2) add new .edmx file to my project and Choose "Generate from database" in wizard

3) Edit DB in .edmx file in Visual Studio (add new table)

4) create new database with "Generate Database from model" in .edmx

5) use Entity Framework Power Tools Beta 3 with "Reverse Engineer Code First"

6) delete existing Database and call MVC action that use my context

Is there a simpler way for this scenario?

I get this error:

Introducing FOREIGN KEY constraint 'FK_dbo.UsersInRoles_dbo.Users_Users_UserId' on table 'UsersInRoles' 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.

please guide me about error and any simple and rapid way for this.

Thanks.

b24
  • 2,425
  • 6
  • 30
  • 51

1 Answers1

1

Looks like you might need to disable "Cascade Delete".

Check this post from this link EF Fluent API

Enabling Cascade Delete

You can configure cascade delete on a relationship by using the WillCascadeOnDelete method. If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null.

You can remove these cascade delete conventions by using:

modelBuilder.Conventions.Remove() modelBuilder.Conventions.Remove()

The following code configures the relationship to be required and then disables cascade delete.

modelBuilder.Entity<Course>()
    .HasRequired(t => t.Department)
    .WithMany(t => t.Courses)
    .HasForeignKey(d => d.DepartmentID)
    .WillCascadeOnDelete(false);

All this should be at your EF Context file.

Note: You might think about the drawbacks of disabling it!

Good luck!

Community
  • 1
  • 1
trinaldi
  • 2,872
  • 2
  • 32
  • 37