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...