• EF Code-First 学习之旅 级联删除


    级联删除是当删除主记录的时候会自动删除依赖的记录或者设置外键属性为null

    public class Student
    {
        public Student() { }
    
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    
        public virtual StudentAddress Address { get; set; }
    
    }
         
    public class StudentAddress 
    {
        [ForeignKey('Student')]
        public int StudentAddressId { get; set; }
            
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public int Zipcode { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    
        public virtual Student Student { get; set; }
    }
    using (var ctx = new SchoolContext()) {
    
        var student1 = new Student() { StudentName = "James" };
        var address1 = new StudentAddress() { Address1 = "address" };
    
        student1.Address = address1;
    
        ctx.Students.Add(student1);
    
        ctx.SaveChanges();
        // student1 and its address will be removed from db
        ctx.Students.Remove(student1);
    
        ctx.SaveChanges();
    }

    级联删除:当删除Student的时候也删除StudentAddress

    级联删除注意的:

      1.需要保证DbContext中已经加载了该父对象的所有子对象

        因此在查询父对象的时候应该使用Include("子对象属性名")查询,

        或者在DbContext另外把其下的所有子对象查询出来

        再进行对父对象的删除方可实现级联删除子对象

    但注意以上所述情况只适用于关联子项比较少的情况,数据量少的演示测试Demo可以,工作中应该杜绝该类解决方案的出现。

    一对多级联删除

    public class Student
    {
        public Student() { }
    
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    
        public virtual Standard Standard { get; set; }
    }
           
    public class Standard
    {
        public Standard()
        {
            Students = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<Student> Students { get; set; }
    }
    using (var ctx = new SchoolContext()) {
    
        var student1 = new Student() { StudentName = "James" };
        var student2 = new Student() { StudentName = "Gandhi" };
    
        var standard1 = new Standard() { StandardName = "Standard 1" };
    
        student1.Standard = standard1;
        student2.Standard = standard1;
    
        ctx.Students.Add(student1);
        ctx.Students.Add(student2);
                    
        //inserts students and standard1 into db
        ctx.SaveChanges();
    
        //deletes standard1 from db and also set standard_StandardId FK column in Students table to null for
        // all the students that reference standard1.
        ctx.Standards.Remove(standard1);
    
        ctx.SaveChanges();
    }

    关闭级联删除

    public class SchoolContext<: DbContext
    {
        public SchoolContext():base("MySchool")
        {
                    }
    
        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
            
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Student>()
                .HasOptional<Standard>(s => s.Standard)
                .WithMany()
                .WillCascadeOnDelete(false);
        }
    }
  • 相关阅读:
    Mandriva Flash: 到处可用的口袋 Linux
    Pidgin 衔接 Google Talk 的设置配备铺排
    5 个使 Vim 更易用的脚本
    Bash 运用才干大补贴
    915Resolution打点宽屏表现标题问题
    Linux下误删root目次
    惠普推出 Linux 迷你条记本
    weblogic8.1for linux ES3.0拆卸与设置
    英俊的 Linux Mini PC
    Zonbu-售价 99 美元的袖珍电脑
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6642303.html
Copyright © 2020-2023  润新知