Eyu.DatabaseAccessor
1.0.7
dotnet add package Eyu.DatabaseAccessor --version 1.0.7
NuGet\Install-Package Eyu.DatabaseAccessor -Version 1.0.7
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.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Eyu.DatabaseAccessor" Version="1.0.7" />
<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.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Eyu.DatabaseAccessor, 1.0.7"
#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.7
#tool nuget:?package=Eyu.DatabaseAccessor&version=1.0.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Eyu.DatabaseAccessor
介绍
一个基于EF Core的数据库访问类库
更新记录
1.0.6
- 优化信号量锁,解决多线程问题
- 实现AutoConfigEntityAttribute,支持使用特性注册实体。
1.0.5
- 实现UnitOfWork,通过UnitOfWork生成的repository都是同一个dbcontext的。
1.0.3
- 增加构造函数初始化
- 支持AddDatabasePoolFactory注入
- IRepository现在支持自定义DbContext
使用说明
创建模型
模型只需要继承EntityBase<T>即可生成int类型的主键以及CreateTime,UpdateTime,DeleteTime等列. 如需不需要默认的列,可继承IEntityBase。 或使用AutoConfigEntityAttibute特性,支持指定设置类和指定dbcontext。
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(UserConfiguration), typeof(DefaultDbContext))]// 指定设置类和DbContext类型
public class User
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
}
public class UserConfiguration : IEntityTypeConfiguration<User>
{
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);
- 更多个性化要求,可自定义DbContext继承BaseDbContext;如果是使用特性注册,重写OnModelCreating()并调用ConfigAutoConfigEntityAttribute方法;如果是继承IEntitybase,则重写OnModelCreating()并调用ConfigIAutoConfigurationAbleEntity<>方法
其他 请参考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 (>= 8.0.16)
- Newtonsoft.Json (>= 13.0.3)
-
net9.0
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.6)
- 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.