Biwen.EFCore.SoftDelete 1.2.0

dotnet add package Biwen.EFCore.SoftDelete --version 1.2.0                
NuGet\Install-Package Biwen.EFCore.SoftDelete -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Biwen.EFCore.SoftDelete" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Biwen.EFCore.SoftDelete --version 1.2.0                
#r "nuget: Biwen.EFCore.SoftDelete, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Biwen.EFCore.SoftDelete as a Cake Addin
#addin nuget:?package=Biwen.EFCore.SoftDelete&version=1.2.0

// Install Biwen.EFCore.SoftDelete as a Cake Tool
#tool nuget:?package=Biwen.EFCore.SoftDelete&version=1.2.0                

Biwen.EFCore.SoftDelete

GitHub license PRs Welcome

  • 实现DbContext的软删除功能

NuGet 包

  • dotnet add package Biwen.EFCore.SoftDelete --version 1.2.0

开发环境

运行环境

Easy to Use

Step 1 Model定义


[PrimaryKey("Id")]
public class Blog : ISoftDeleted
{
    //...
    public int Id { get; set; }
    public string Title { get; set; } = null!;
    public string Content { get; set; } = null!;
    public int AuthorId { get; set; }

    //请注意这里的IsDeleted默认必须是false!表示未删除
    public bool IsDeleted { get; set; } = false;
}

Step 2 继承SoftDeleteDbContext


public class TestDbContext : SoftDeleteDbContext
{
	public TestDbContext(DbContextOptions<TestDbContext> options)
		: base(options)
	{
	}
    //...
	public DbSet<Blog> Blogs { get; set; }

	protected override void OnModelCreating(ModelBuilder builder)
	{
		base.OnModelCreating(builder);
		//根据情况自定义表名
        //...
	}
}

step 2 注册DbContext


var serviceProvider = new ServiceCollection()
  .AddDbContext<TestDbContext>(options =>
  {
	    //使用你的数据库引擎
      options.UseInMemoryDatabase("test");
  })
  .BuildServiceProvider();

step 3 注意事项


//1. 请使用 DbSet.Remove() 方法,不可使用批量删除方法:ExecuteDelete(),ExecuteDeleteAsync()


Enjoy !


//Delete 1 模拟软删除
var blog1 = db.Blogs.Find(1);
db.Remove(blog1);
db.SaveChanges();

//Delete 3 模拟强制删除
var blog3 = db.Blogs.Find(3);
db.Remove(blog3, true);
db.SaveChanges();

License

  • MIT

联系我

  • QQ:552175420
  • Email: vipwan#outlook.com

项目地址

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 81 12/4/2024
1.1.1 172 9/12/2023
1.1.0 164 9/11/2023
1.0.0 175 9/11/2023 1.0.0 is deprecated because it is no longer maintained.

提供强制删除扩展