WillCascadeOnDelete RRS feed

  • Question

  • Hello,
    I setup a cascading deletion
    HasMany<gLine>(p => p.Lines).WithRequired(l => l.Document).HasForeignKey(l => new { l.GL_NATUREPIECEG, l.GL_SOUCHE, l.GL_NUMERO, l.GL_INDICEG })
    but it is only effective if I load the cascading entities.
    The following delete with cascade :
    gDoc = (from x in gdc.Entities.Include("Lines") where x.GP_SOUCHE == "ZZZ" select x).FirstOrDefault();
    The following one fails to delete with cascade :
    gDoc = (from x in gdc.Entities where x.GP_SOUCHE == "ZZZ" select x).FirstOrDefault();
    Is it the normal ? Do I miss something ?
    thank you in advance

    • Edited by thierry schmit Saturday, August 27, 2011 12:41 PM spacing for clarity
    Friday, August 26, 2011 11:04 PM

All replies

  • I found here that this behavior used to be a bug:


    Is it still a a bug ?

    Saturday, August 27, 2011 1:16 PM
  • Honestly, i didint repro your question on my side. I added this to create new databasev
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
          modelBuilder.Entity<Student>().HasRequired(a => a.Teacher).WithMany().HasForeignKey(b => b.TeacherId).WillCascadeOnDelete(true);

          using (Test2Entities ctx = new Test2Entities())
            var query = (from p in ctx.Students.Include("Teacher")
                   where p.TeacherId == 1
                   select p).ToList();
            foreach (var i in query)

    can you provide more example to us?



    Just a newbie for everything.
    Tuesday, August 30, 2011 11:45 AM
  • hello,

    thank you for your answer.

    if I well understand it you want to destroy students when you destroy a teacher ?

    by the way, you use Include, what if not ?

    thank you in advance

    Tuesday, August 30, 2011 2:32 PM
  • you are right. i delete records from student and concurrently i deleted record from teacher table.

    perhaps you provide code fragment how did you create database or demo applet here.



    Just a newbie for everything.
    Wednesday, August 31, 2011 1:47 AM