zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    依赖注入与控制反转
    Java NIO(三)
    Java NIO(二)
    Java NIO(一)
    Jetty 的基本架构
    spring IOC
    java代理
    spring AOP
    linux 添加用户并授权可以操作文件夹
    Centos6.5 yum 安装jdk1.8
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6642303.html
Copyright © 2011-2022 走看看