Biwen.EFCore.SoftDelete 1.1.1

dotnet add package Biwen.EFCore.SoftDelete --version 1.1.1                
NuGet\Install-Package Biwen.EFCore.SoftDelete -Version 1.1.1                
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.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Biwen.EFCore.SoftDelete --version 1.1.1                
#r "nuget: Biwen.EFCore.SoftDelete, 1.1.1"                
#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.1.1

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

Biwen.EFCore.SoftDelete

GitHub license PRs Welcome

  • 实现DbContext的软删除功能

NuGet 包

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

开发环境

运行环境

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)
	{
           //使用软删除
           this.UseSoftDelete();
	}
    //...
	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.FirstOrDefault(x => x.Id == 1);
db.Remove(blog1!);
db.SaveChanges();

//Delete 3 模拟强制删除
var blog3 = db.Blogs.FirstOrDefault(x => x.Id == 3);
db.Remove(blog3!, true);
db.SaveChanges();

License

  • MIT

联系我

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

项目地址

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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.1.1 166 9/12/2023
1.1.0 158 9/11/2023
1.0.0 169 9/11/2023 1.0.0 is deprecated because it is no longer maintained.

提供强制删除扩展