Eyu.DatabaseAccessor
1.0.6-beta4
This is a prerelease version of Eyu.DatabaseAccessor.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Eyu.DatabaseAccessor --version 1.0.6-beta4
NuGet\Install-Package Eyu.DatabaseAccessor -Version 1.0.6-beta4
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="Eyu.DatabaseAccessor" Version="1.0.6-beta4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Eyu.DatabaseAccessor" Version="1.0.6-beta4" />
<PackageReference Include="Eyu.DatabaseAccessor" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Eyu.DatabaseAccessor --version 1.0.6-beta4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Eyu.DatabaseAccessor, 1.0.6-beta4"
#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.
#addin nuget:?package=Eyu.DatabaseAccessor&version=1.0.6-beta4&prerelease
#tool nuget:?package=Eyu.DatabaseAccessor&version=1.0.6-beta4&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Eyu.DatabaseAccessor
介绍
一个基于EF Core的数据库访问类库
更新记录
1.0.6
- 优化信号量锁,解决多线程问题
1.0.5
- 实现UnitOfWork,通过UnitOfWork生成的repository都是同一个dbcontext的。
1.0.3
- 增加构造函数初始化
- 支持AddDatabasePoolFactory注入
- IRepository现在支持自定义DbContext
使用说明
创建模型
模型只需要继承EntityBase<T>即可生成int类型的主键以及CreateTime,UpdateTime,DeleteTime等列. 如需不需要默认的列,可继承IEntityBase。 或模型实现IEntityTypeConfiguration<>接口,并附加AutoConfigEntityAttibute特性,可实现更多个性化。
public class Class : EntityBase<Class>
{
public string Name { get; set; }
public string Address { get; set; }
public List<Student> Students { get; set; }
}
public class Student: EntityBase<Student>
{
public string Name { get; set; }
public int ClassId { get; set; }
public Class Class { get; set; }
public override void Configure(EntityTypeBuilder<Student> builder)
{
builder.HasOne(s => s.Class).WithMany(c => c.Students).HasForeignKey(s => s.ClassId);
}
}
[AutoConfigEntity(typeof(User))]
public class User : IEntityTypeConfiguration<User>
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Address { get; set; }
public string City { get; set; }
public void Configure(EntityTypeBuilder<User> builder)
{
}
}
DbContext配置和初始化。
- 依赖注入
// 注册
var service = new ServiceCollection();
var connectString = new SqliteConnectionStringBuilder { DataSource = "database.db", Mode = SqliteOpenMode.ReadWriteCreate }.ToString();
service.AddDatabasePool(options =>
{
options.UseSqlite(connectString,x => x.MigrationsAssembly([Your assembly fullName]));
});
var serviceProvider = service.BuildServiceProvider().CreateScope().ServiceProvider;
// 使用仓库类
var repository = serviceProvider.GetRequiredService<IRepository<School>>();
- 构造器初始化。
var connectString = new SqliteConnectionStringBuilder { DataSource = "database.db", Mode = SqliteOpenMode.ReadWriteCreate }.ToString();
var repository = DatabaseExtension.BuildRepository<School>(options=>{options.UseSqlite(connectString);});
数据库迁移及初始化
创建dbcontext时传入枚举InitDatabaseMode, 有三种模式
- Create 构建模式:如果数据库不存在,则构建数据库以及表。
- Migrate 迁移模式:如果数据库不存在,则创建数据库以及表。如果数据库存在,但迁移未应用,则运行迁移代码应用迁移。
- None 无初始化模式:适用于已存在的数据库以及表,并且不需要进行迁移。
- 依赖注入
var service = new ServiceCollection();
var connectString = new SqliteConnectionStringBuilder { DataSource = "database.db", Mode = SqliteOpenMode.ReadWriteCreate }.ToString();
service.AddDatabasePool(options =>
{
options.UseSqlite(connectString,x => x.MigrationsAssembly([Your assembly fullName]));
}, InitDatabaseMode.Create);
- 构造器
var connectString = new SqliteConnectionStringBuilder { DataSource = "database.db", Mode = SqliteOpenMode.ReadWriteCreate }.ToString();
var context = DatabaseExtension.BuildDbContext(options =>{ options.UseSqlite(connectString); },InitDatabaseMode.Create);
var repository = DatabaseExtension.BuildRepository<School>(options=>{options.UseSqlite(connectString);},InitDatabaseMode.Create);
其他 请参考ef core
Product | Versions 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. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.2)
- Newtonsoft.Json (>= 13.0.3)
-
net9.0
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.2)
- Newtonsoft.Json (>= 13.0.3)
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.0.7 | 64 | 6/21/2025 |
1.0.6 | 134 | 6/18/2025 |
1.0.6-beta4 | 259 | 4/9/2025 |
1.0.5 | 120 | 2/17/2025 |