JfYu.Data
8.2.0
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 JfYu.Data --version 8.2.0
NuGet\Install-Package JfYu.Data -Version 8.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="JfYu.Data" Version="8.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JfYu.Data" Version="8.2.0" />
<PackageReference Include="JfYu.Data" />
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 JfYu.Data --version 8.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: JfYu.Data, 8.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.
#:package JfYu.Data@8.2.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JfYu.Data&version=8.2.0
#tool nuget:?package=JfYu.Data&version=8.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EF Read-Write Separation
MySql,SqlServer,Sqlite
Install-Package JfYu.Data
Configuration
"ConnectionStrings": {
"DatabaseType": "SqlServer",
"ConnectionString": "Data Source = 127.0.0.1,9004; database = dbtest; User Id = sa; Password = 123456;Encrypt=True;TrustServerCertificate=True;",
"JfYuReadOnly":"JfYuReadOnly",
"ReadOnlyDatabases": [
{
"DatabaseType": "MySql",
"ConnectionString": "server=127.0.0.1;userid=root;pwd=123456;port=9001;database=dbtest;"
},
{
"DatabaseType": "Sqlite",
"ConnectionString": "Data Source= data/m2.db;Password = 123456;"
}
]
}
Create DbContext
public class User : BaseEntity
{
/// <summary>
/// UserName
/// </summary>
[DisplayName("UserName"), Required, MaxLength(100)]
public required string UserName { get; set; }
/// <summary>
/// NickName
/// </summary>
[DisplayName("NickName"), Required, MaxLength(100)]
public string? NickName { get; set; }
/// <summary>
/// DepartmentId
/// </summary>
[DisplayName("DepartmentId")]
public int? DepartmentId { get; set; }
/// <summary>
/// Department
/// </summary>
public virtual Department? Department { get; set; }
}
public class Department : BaseEntity
{
/// <summary>
/// Name
/// </summary>
[DisplayName("Name"), Required]
public required string Name { get; set; }
/// <summary>
/// SubName
/// </summary>
[DisplayName("SubName"), Required]
public required string SubName { get; set; }
/// <summary>
/// SuperiorId
/// </summary>
[DisplayName("SuperiorId")]
public int? SuperiorId { get; set; }
/// <summary>
/// Superior
/// </summary>
[DisplayName("Superior")]
public virtual Department? Superior { get; set; }
/// <summary>
/// Users
/// </summary>
[DisplayName("Users")]
public virtual List<User>? Users { get; set; }
}
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Department> Departments { get; set; }
}
//Design Factory
class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
public DataContext CreateDbContext(string[] args)
{
var connectionString = Environment.GetEnvironmentVariable("EFConString");
if (string.IsNullOrEmpty(connectionString))
throw new InvalidOperationException("The connection string was not set in the 'EFConString' environment variable.");
var optionsBuilder = new DbContextOptionsBuilder<DataContext>();
optionsBuilder.UseSqlServer(connectionString);
//optionsBuilder.UseMySql(connectionString,ServerVersion.AutoDetect(connectionString));
return new DataContext(optionsBuilder.Options);
}
}
Migration
//set migration connection string
$env:EFConString="Data Source = xxx; database = test; User Id = sa; Password = xxx;";
//create migration
dotnet ef migrations add init --project XXXX
//update database
dotnet ef database update --project XXXX
if encountered runtime error add following package
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="x.x.x" />
or
Install-Package Microsoft.EntityFrameworkCore.Tools
or
dotnet add package Microsoft.EntityFrameworkCore.Tools
Injection
services.AddJfYuDbContextService<DataContext>(q =>
{
q.ConnectionString = "server=127.0.0.1;Database=Test;uid=Test;pwd=test;";
q.ReadOnlyDatabases = new List<DatabaseConfig>() { new DatabaseConfig() { ConnectionString = "server=127.0.0.2;Database=Test;uid=Test;pwd=test;" } };
});
services.AddJfYuDbContextService<DataContext>(options =>
{
configuration.GetSection("ConnectionStrings").Bind(options);
});
Usage
//master
var _masterContext = serviceProvider.GetService<DataContext>();
//readonly
serviceProvider.GetService<ReadonlyDBContext<DataContext>>();
//service
var _companyService=serviceProvider.GetService<IService<Company, DataContext>>();
await _companyService.AddAsync(new Company() { Age = 33, Name = "test" }
await _companyService.UpdateAsync(data)
await _companyService.RemoveAsync(q => q.ID.Equals(data.ID))
_companyService.GetList(q => q.Name == "test124")
Expand
public interface IUserService : IService<User, DataContext>
{
Task<User?> GetByNickNameAsync(string nickName);
}
public class UserService(DataContext context, ReadonlyDBContext<DataContext> readonlyDBContext)
: Service<User, DataContext>(context, readonlyDBContext), IUserService
{
public async Task<User?> GetByNickNameAsync(string nickName)
{
return await Context.Set<User>().FirstOrDefaultAsync(u => u.NickName == nickName);
}
}
//IOC
services.AddScoped<IUserService, UserService>();
| 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 was computed. 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 (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Design (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Proxies (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Sqlite (>= 8.0.8)
- Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Tools (>= 8.0.8)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Pomelo.EntityFrameworkCore.MySql (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.