Aicrosoft.DataAccess.EntityFrameworkCore 8.5.0

dotnet add package Aicrosoft.DataAccess.EntityFrameworkCore --version 8.5.0                
NuGet\Install-Package Aicrosoft.DataAccess.EntityFrameworkCore -Version 8.5.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="Aicrosoft.DataAccess.EntityFrameworkCore" Version="8.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aicrosoft.DataAccess.EntityFrameworkCore --version 8.5.0                
#r "nuget: Aicrosoft.DataAccess.EntityFrameworkCore, 8.5.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 Aicrosoft.DataAccess.EntityFrameworkCore as a Cake Addin
#addin nuget:?package=Aicrosoft.DataAccess.EntityFrameworkCore&version=8.5.0

// Install Aicrosoft.DataAccess.EntityFrameworkCore as a Cake Tool
#tool nuget:?package=Aicrosoft.DataAccess.EntityFrameworkCore&version=8.5.0                

Aicrosoft.DataAccess.EntityFrameworkCore

本库引用了 Aicrosoft.DataAccess ,使得可以直接通过IOC取得位于该库的 实现了接口IRepository<Entity, Pkey>的实例,来操作相关Db。

实体类的设计说明

  • 一般情况下不设计外键约束,约束关系由程序处理。除非特殊的表结构才交由Db处理约束关系;
  • 一个表实体是一个内聚的,如果与别的表有关系,最好再加一个关系表。比如User和Role的关系表叫UserRole;
  • 复杂的实体(比有一个属性引用的是另一个表的实体或集合)对开发人员要求太高,容易出问题。所以最好只是简单的表关系,由业务逻辑来保护数据的完整性。
  • DbContext的实现为: class XxxxDbContext(DbContextOptions options) : DbContext(options);
  • 实体表继承Entity<T>,默认了一个为Id的主键,如果有默认值,最好直接在属性上书写(即构造时生成),因为不同的Db有不同的取默认值方式;
  • 实体表如果要自定义主键或组合主键,不需要继承Entity<T>
  • [可以不加]索引设置格式为:.HasDatabaseName("IX_TableName_ColumnName"),不加时会自动生成该形式的结构;

-------- 以下待整理 -----------------

EF Migration doc

重要接口

IRepository<TEntity, TKey> 针对DbContext中的某个表的实体的存储操作

  • TEntity 传入的实体
  • TKey 实体的Key

ICommonRepository

  • 无关于实体的仅和DB有关的相关操作

Note

  • 连接数是与 DbContext 的实例数量相关的。

How To Use

  1. Add Entity For Table
public class Phone : Entity<Guid>
{
    public string? PhoneNumber { get; set; }

}

public class Employe : Entity<int>
{
    public string? Name { get; set; }

    public List<Phone>? Phones { get; set; }
}
  1. First Create XXDbContext for Database
public class SqlServerSampleDbContext : DbContext
{
    //可初始化连接串
    public SqlServerSampleDbContext(DbContextOptions options) : base(options) { }

    public DbSet<Employe>? Employes { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        ////它会重重通过DbContextOptions传进来的连接串。
        // optionsBuilder.UseSqlServer("MyConnectionString")
    }
}

  1. Migration and update database 不必是在可启动的项目里,只要是在有DbContext的项目中即可。
# Install
dotnet tool install --global dotnet-ef

# update
dotnet tool update --global dotnet-ef

# cd the project dir
## 专门用于迁移Sqliter的Client端
cd F:\Aicrosoft\ProjectTemplates\ProjectTemplates\Developing\SuperJobs\SqliteDbSampeMigrator

# use the tools on a specific project || 用Nuget包安装到你想生成的项目里
# 注意:版本必须与DbContext引用的版本一致。
dotnet add package Microsoft.EntityFrameworkCore.Design -v 8.0.7
# Install-Package Microsoft.EntityFrameworkCore.Design -Version 8.0.7

# add migration
## 创建一个新的迁移脚本,记录当前模型与数据库之间的差异。
dotnet ef migrations add init --context SqliteSampleDbContextMig

# Updates the database to the last migration or to a specified migration.
## 将所有未应用的迁移应用到数据库上。
dotnet ef database update --context SqliteSampleDbContext


## 显示所有可用的迁移,包括已应用和未应用的迁移。
dotnet ef migrations list

## 回滚最近应用的迁移。
dotnet ef database update <MigrationName>

## 生成SQL脚本来创建数据库和所有迁移。
dotnet ef migrations script
  1. Use by IEFRepository
  var rep = serviceProvider.GetRequiredService<IRepository<User, int>>();
  rep.Add(new User { Name = "Test" });
  var rst = rep.SaveChanges();


Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Aicrosoft.DataAccess.EntityFrameworkCore:

Package Downloads
Aicrosoft.DataAccess.DbMigration

Extensions of Aicrosoft Ltd.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.5.0 86 10/12/2024
8.3.0 102 9/10/2024
8.2.0 93 8/7/2024
8.1.0 95 7/16/2024
8.1.0-dev.240716.12 48 7/16/2024
6.4.0 311 11/20/2023
6.3.2 147 11/1/2023
6.2.1 160 8/25/2023
6.1.7 192 7/25/2023
6.1.6 160 5/17/2023