none
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 })
        .WillCascadeOnDelete(true);
    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();
    gdc.DeleteEntity(gDoc);
    
    The following one fails to delete with cascade :
    gDoc = (from x in gdc.Entities where x.GP_SOUCHE == "ZZZ" select x).FirstOrDefault();
    gdc.DeleteEntity(gDoc);
    Is it the normal ? Do I miss something ?
    thank you in advance

    thierry
    • 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:

    http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/05ef23d7-4bc9-455f-9bbd-aa32c447bd7b

    Is it still a a bug ?


    thierry
    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)
            {
              ctx.DeleteObject(i);
            }
            ctx.SaveChanges();
          }
    

    can you provide more example to us?

    Thanks,

    Werewolf,


    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


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

    Thank,

    Werewolf,


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