Skip to content

Commit 8111740

Browse files
committed
added example for cascade delete
1 parent 416b23e commit 8111740

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\FileContextCore\FileContextCore.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

CascadeExample/Data/Base.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace CascadeExample.Data
4+
{
5+
public class Base
6+
{
7+
[Key]
8+
public int Id { get; set; }
9+
}
10+
}

CascadeExample/Data/Db.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using FileContextCore;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace CascadeExample.Data
5+
{
6+
public class Db : DbContext
7+
{
8+
public DbSet<User> Users { get; set; }
9+
10+
public DbSet<Entry> Entries { get; set; }
11+
12+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
13+
{
14+
optionsBuilder.UseFileContextDatabase();
15+
}
16+
}
17+
}

CascadeExample/Data/Entry.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ComponentModel.DataAnnotations.Schema;
2+
3+
namespace CascadeExample.Data
4+
{
5+
public class Entry : Base
6+
{
7+
public string Content { get; set; }
8+
9+
[ForeignKey("User")]
10+
public int UserId { get; set; }
11+
12+
public virtual User User { get; set; }
13+
}
14+
}

CascadeExample/Data/User.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace CascadeExample.Data
4+
{
5+
public class User : Base
6+
{
7+
public string Name { get; set; }
8+
9+
public virtual List<Entry> Entries { get; set; }
10+
}
11+
}

CascadeExample/Program.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using CascadeExample.Data;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace CascadeExample
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
Db db = new Db();
14+
15+
// List<User> users = db.Users.ToList();
16+
17+
// db.Users.Add(new User()
18+
// {
19+
// Name = "Test user",
20+
// Entries = new List<Entry>()
21+
// {
22+
// new Entry() { Content = "Entry 1" },
23+
// new Entry() { Content = "Entry 2" }
24+
// }
25+
// });
26+
// db.SaveChanges();
27+
28+
// List<User> users2 = db.Users.Include(u => u.Entries).ToList();
29+
//
30+
db.Users.RemoveRange(db.Users.Include(u => u.Entries));
31+
db.SaveChanges();
32+
}
33+
}
34+
}

FileContextCore.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "Example\Example.
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASPNETDemo", "ASPNETDemo\ASPNETDemo.csproj", "{8E6BFB30-5AF1-46EF-81B7-A8200616E45A}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CascadeExample", "CascadeExample\CascadeExample.csproj", "{3D51BF95-B38E-4221-AAAE-5D093FD163DA}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{8E6BFB30-5AF1-46EF-81B7-A8200616E45A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{8E6BFB30-5AF1-46EF-81B7-A8200616E45A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{8E6BFB30-5AF1-46EF-81B7-A8200616E45A}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{3D51BF95-B38E-4221-AAAE-5D093FD163DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{3D51BF95-B38E-4221-AAAE-5D093FD163DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{3D51BF95-B38E-4221-AAAE-5D093FD163DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{3D51BF95-B38E-4221-AAAE-5D093FD163DA}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)