Entity Framework 主键+外键约束与级连删除 Database First

在SQL中先建几张表:

设置为唯一级连删除:在keymain2与keysubse2之间

这么看有点累, 上个关系图就好理解了:

建立数据实体后代码是这样的:

 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //     This code was generated from a template.
 4 //
 5 //     Manual changes to this file may cause unexpected behavior in your application.
 6 //     Manual changes to this file will be overwritten if the code is regenerated.
 7 // </auto-generated>
 8 //------------------------------------------------------------------------------
 9 
10 namespace WebAppNet.Models
11 {
12     using System;
13     using System.Collections.Generic;
14     
15     public partial class KeyMain
16     {
17         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
18         public KeyMain()
19         {
20             this.KeyMain2 = new HashSet<KeyMain2>();
21             this.KeySubsets = new HashSet<KeySubset>();
22         }
23     
24         public System.Guid KeyID { get; set; }
25         public Nullable<System.Guid> KeyType { get; set; }
26         public string KeyValue { get; set; }
27         public string KeyName { get; set; }
28     
29         public virtual KeyType KeyType1 { get; set; }
30         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
31         public virtual ICollection<KeyMain2> KeyMain2 { get; set; }
32         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
33         public virtual ICollection<KeySubset> KeySubsets { get; set; }
34     }
35 }
 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //     This code was generated from a template.
 4 //
 5 //     Manual changes to this file may cause unexpected behavior in your application.
 6 //     Manual changes to this file will be overwritten if the code is regenerated.
 7 // </auto-generated>
 8 //------------------------------------------------------------------------------
 9 
10 namespace WebAppNet.Models
11 {
12     using System;
13     using System.Collections.Generic;
14     
15     public partial class KeyMain2
16     {
17         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
18         public KeyMain2()
19         {
20             this.KeySubset2 = new HashSet<KeySubset2>();
21         }
22     
23         public System.Guid Key2ID { get; set; }
24         public Nullable<System.Guid> KeyID { get; set; }
25         public string Key2Name { get; set; }
26     
27         public virtual KeyMain KeyMain { get; set; }
28         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
29         public virtual ICollection<KeySubset2> KeySubset2 { get; set; }
30     }
31 }
 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //     This code was generated from a template.
 4 //
 5 //     Manual changes to this file may cause unexpected behavior in your application.
 6 //     Manual changes to this file will be overwritten if the code is regenerated.
 7 // </auto-generated>
 8 //------------------------------------------------------------------------------
 9 
10 namespace WebAppNet.Models
11 {
12     using System;
13     using System.Collections.Generic;
14     
15     public partial class KeySubset
16     {
17         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
18         public KeySubset()
19         {
20             this.KeySubset2 = new HashSet<KeySubset2>();
21         }
22     
23         public System.Guid KeySubsetID { get; set; }
24         public Nullable<System.Guid> KeyID { get; set; }
25         public string KeySubsetName { get; set; }
26     
27         public virtual KeyMain KeyMain { get; set; }
28         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
29         public virtual ICollection<KeySubset2> KeySubset2 { get; set; }
30     }
31 }
 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //     This code was generated from a template.
 4 //
 5 //     Manual changes to this file may cause unexpected behavior in your application.
 6 //     Manual changes to this file will be overwritten if the code is regenerated.
 7 // </auto-generated>
 8 //------------------------------------------------------------------------------
 9 
10 namespace WebAppNet.Models
11 {
12     using System;
13     using System.Collections.Generic;
14     
15     public partial class KeySubset2
16     {
17         public System.Guid KeySubset2ID { get; set; }
18         public System.Guid KeySubsetID { get; set; }
19         public string KeySubset2name { get; set; }
20         public Nullable<System.Guid> KeyMain2ID { get; set; }
21     
22         public virtual KeyMain2 KeyMain2 { get; set; }
23         public virtual KeySubset KeySubset { get; set; }
24     }
25 }

 增改查都没什么好说的,我们来看一下设置了为唯一级连删除的地方:在keymain2与keysubse2之间

 这两张表中有各有一条数据,删除子表没什么好说的。

我们来看一下删除主表,当删除主表时:

点击删除,删除成功。同时可以看到子表数据也一起删除了:

 

我们在来删除一下没有设置级连删除的主表:

删除主表报错。

我们来分析一下两种做法优缺点:

1.设置了级连删除

   优点:删除主表时很方便的同时删除了子表,不需要写过多的代码,逻辑也是自动处理的。

   缺点:可能会不小心删除很多关连的数据,造成数据的误删。

2.未设置了级连删除(数据库默认)

   优点:删除逻辑清楚,删除主表时,子表有数据不能直接删除主表【因为会报错】。需要自己手动写逻辑,容易理清数据之间的关系。

   缺点:所有删除逻辑都需要自己手写。

我们通过项目中的需求和实际情况,来考虑如何使用这些功能。

原文地址:https://www.cnblogs.com/cxd1008/p/7116679.html