1

I use two entities.

public class Reservation
    {
        public Reservation()
        {
        }

        [Display(Name = "Id:")]
        [Required]
        public int Id { get; set; }


        public string ClientId { get; set; }
        public virtual User Client { get; set; }

    }

public class User : IdentityUser
{
    public User()
    {
        this.Reservations = new HashSet<Reservation>();
    }

    [Required]
    [Display(Name = "Imię")]

    public string Name { get; set; }
    [Display(Name = "Nazwisko")]
    [Required]
    public string Surname { get; set; }

    [Display(Name = "Data urodzenia")]
    [DataType(DataType.Date)]
    [Required]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime BirthDate { get; set; }

    public virtual ICollection<Reservation> Reservations { get; private set; }
}

When I want to delete User, addtionally I would like to remove all reservations assigned to this User. So in context class I have written.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            modelBuilder.Entity<Reservation>().HasRequired(x => x.Client)
                                            .WithMany(x => x.Reservations)
                                            .HasForeignKey(x => x.ClientId)
                                            .WillCascadeOnDelete(true);
}

Then during the migration it shows me error described in the title of issue. I cannot remove Convetions remove one to many, it is essentail for me. How to solve this?

I have changed on it.

modelBuilder.Entity<Reservation>().HasRequired(x => x.Client)
                                            .WithMany(x => x.Reservations)
                                            .HasForeignKey(x => x.ClientId);

This when I want to remove User.

Message = "The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Reservation_dbo.AspNetUsers_ClientId\". The conflict occurred in database \"aspnet-AgrotouristicWebApplication-20170716115104\", table \"dbo.Reservation\", column 'ClientId'.\r\nThe sta...
maciejka
  • 818
  • 2
  • 17
  • 42
  • 2
    Possible duplicate of [Entity Framework Code first: cycles or multiple cascade paths](https://stackoverflow.com/questions/29062094/entity-framework-code-first-cycles-or-multiple-cascade-paths) – jegtugado Oct 05 '17 at 08:05
  • Is the first problem (multiple cascade paths) solved by removing `.WillCascadeOnDelete(true)`? If it's solved already, can you provide example how to delete `Reservation` entity data? – Tetsuya Yamamoto Oct 05 '17 at 09:05
  • I have solved problem by removing all Reservations assiosiated with User before removing User itself. – maciejka Oct 05 '17 at 09:12

0 Answers0