EasilyNET.EntityFrameworkCore
1.9.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 EasilyNET.EntityFrameworkCore --version 1.9.0
NuGet\Install-Package EasilyNET.EntityFrameworkCore -Version 1.9.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="EasilyNET.EntityFrameworkCore" Version="1.9.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.EntityFrameworkCore --version 1.9.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.EntityFrameworkCore, 1.9.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 EasilyNET.EntityFrameworkCore as a Cake Addin #addin nuget:?package=EasilyNET.EntityFrameworkCore&version=1.9.0 // Install EasilyNET.EntityFrameworkCore as a Cake Tool #tool nuget:?package=EasilyNET.EntityFrameworkCore&version=1.9.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasilyNET.EntityFrameworkCore
Install Package
Install-Package EasilyNET.EntityFrameworkCore
创建实体
public sealed class User : Entity<long>, IAggregateRoot, IMayHaveCreator<long?>, IHasCreationTime, IHasModifierId<long?>, IHasModificationTime, IHasDeleterId<long?>, IHasDeletionTime
{
private User() { }
public User(string name, int age)
{
Name = name;
Age = age;
}
public string Name { get; private set; } = default!;
public int Age { get; }
/// <inheritdoc />
public DateTime CreationTime { get; set; }
/// <inheritdoc />
public long? DeleterId { get; set; }
/// <inheritdoc />
public DateTime? DeletionTime { get; set; }
/// <inheritdoc />
public DateTime? LastModificationTime { get; set; }
/// <inheritdoc />
public long? LastModifierId { get; set; }
/// <inheritdoc />
public long? CreatorId { get; set; }
public void ChangeName(string name)
{
Name = name;
}
}
1.Entity 实体需要什么类型Id请自己控制必须的。
2.IAggregateRoot 聚合根假如使用DDD模式的话,请加上这个,不是必须的请自己的需求来
3.IMayHaveCreator 创建人ID,可空的
4.IHasCreationTime 创建时间
5.IHasModifierId 修改人ID
6.IHasModificationTime 修改时间
7.IHasDeleterId 软删除用户ID
8.IHasDeletionTime、IHasSoftDelete 软删除时间,继承IHasSoftDelete,假如需要使用软删除的请就可以了。
以上1.是必须的,请按自己的需要
实体映射
public class UserConfiguration : IEntityTypeConfiguration<User>
{
/// <inheritdoc />
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Name).IsRequired().HasMaxLength(50);
builder.ToTable("User");
}
}
以上那些继承接口,自动映射。
EF 上下文
public sealed class TestDbContext : DefaultDbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options, IServiceProvider? serviceProvider)
: base(options, serviceProvider)
{
Database.EnsureCreated(); //不是必须的,按自己方式,最好使用命令迁移
}
}
注册服务
AddEFCore<TestDbContext>(options => options.ConfigureDbContextBuilder = builder => { builder.UseSqlite("Data Source=My.db"); });
AddEFCore 方法里面,只注入工作单元,假如要使用仓储请自行注入
仓储使用
仓储有两种方式
1.
注入:
AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
使用
_serviceProvider.GetService<IRepository<User, long>>();
最新在构造函数下
IRepository<User, long>
创建:
public interface IUserRepository : IRepository<User, long>;
/// <summary>
/// UserRepository
/// </summary>
/// <param name="dbContext"></param>
public class UserRepository(TestDbContext dbContext) : RepositoryBase<User, long, TestDbContext>(dbContext), IUserRepository;
注入:
AddScoped<IUserRepository, UserRepository>();
Product | Versions 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 is compatible. 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 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- EasilyNET.Core.Domains (>= 1.9.0)
- Microsoft.EntityFrameworkCore (>= 6.0.23)
- Microsoft.EntityFrameworkCore.Relational (>= 6.0.23)
-
net7.0
- EasilyNET.Core.Domains (>= 1.9.0)
- Microsoft.EntityFrameworkCore (>= 7.0.12)
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.12)
-
net8.0
- EasilyNET.Core.Domains (>= 1.9.0)
- Microsoft.EntityFrameworkCore (>= 8.0.0-rc.2.23480.1)
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.0-rc.2.23480.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.