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);
        }
    }
  • 相关阅读:
    ORACLE 查看进程数,已执行任务数, 剩余任务数,删除指定任务
    ORACLE 收集统计整个用户数据
    解决Hystrix dashboard Turbine 一直 Loading…… 及其他坑
    利用 Maven 构造 Spring Cloud 微服务架构 模块使用 spring Boot构建
    AES加解密
    JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案
    JAVA 文件的上传下载
    shell启停服务脚本模板
    JAVA 设计模式之 原型模式详解
    JAVA 设计模式之 工厂模式详解
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6642303.html
Copyright © 2011-2022 走看看