ArielCore.MixedDataBase 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ArielCore.MixedDataBase --version 1.0.1
                    
NuGet\Install-Package ArielCore.MixedDataBase -Version 1.0.1
                    
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="ArielCore.MixedDataBase" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ArielCore.MixedDataBase" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ArielCore.MixedDataBase" />
                    
Project file
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 ArielCore.MixedDataBase --version 1.0.1
                    
#r "nuget: ArielCore.MixedDataBase, 1.0.1"
                    
#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 ArielCore.MixedDataBase@1.0.1
                    
#: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=ArielCore.MixedDataBase&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ArielCore.MixedDataBase&version=1.0.1
                    
Install as a Cake Tool

使用步骤:

  1. 首先添加nuget包ArielCore.MixedDataBase
  2. 在startup.cs或Program.cs或入口处添加如下
 services.AddCoreDbContext<MySqlSjDbContext>("server=localhost;port=3306;user id=root;password=123456;database=cztest;charset=utf8mb4;ConnectionReset=false;AllowUserVariables=true;default command timeout=600;", DataBaseType.MySql);
  1. 添加文件MySqlSjDbContext.cs文件
  namespace API.Domain.DataAcces
  {
      /// <summary>
      /// DbContext
      /// </summary>
      public class MySqlSjDbContext : DbContext
      {
          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="options"></param>
          public MySqlSjDbContext(DbContextOptions options) : base(options)
          {
              Database.EnsureCreated();
          }
          /// <summary>
          /// 用户
          /// </summary>
          public DbSet<UserEntity> Users { get; set; }
          /// <summary>
          /// OnModelCreating
          /// </summary>
          /// <param name="modelBuilder"></param>
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
              #region 注册领域模型与数据库的映射关系
              //modelBuilder.ApplyConfiguration(new UserSourceEntityTypeConfiguration());
              #endregion
          }
      }
  }
  1. 添加文件UserEntity.cs
namespace API.Domain.Entities.UserAggregate
{
    /// <summary>
    /// 用户
    /// </summary>
    [Table("t_user")]
    public class UserEntity : CommonBaseEntity
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserEntity() { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="loginName"></param>
        public UserEntity(string loginName)
        {
            LoginName = loginName;
            UserName = LoginName;
            CreatedTime = DateTime.Now;
            UpdatedTime = DateTime.Now;
            IsDeleted = false;
        }
        /// <summary>
        /// 用户名称
        /// </summary>
        [Required]
        [MaxLength(50)]
        public string UserName { get; set; }
        /// <summary>
        /// 登录名称
        /// </summary>
        [Required]
        [MaxLength(50)]
        public string LoginName { get; set; }
        /// <summary>
        /// 登录密码
        /// </summary>
        [Required]
        [MaxLength(150)]
        public string LoginPwd { get; set; }
        /// <summary>
        /// Email
        /// </summary>
        [MaxLength(50)]
        public string Email { get; set; }
        #region 关联属性,有需要类似下面添加
        /// <summary>
        /// 用户-部门
        /// </summary>
        [NotMapped]
        public virtual ICollection<UserDepartmentEntity> UserDepartments { get; set; }
        /// <summary>
        /// 部门id集合
        /// </summary>
        [NotMapped]
        public List<string> DepartmentIds { get; set; }
        /// <summary>
        /// 部门名称集合
        /// </summary>
        [NotMapped]
        public List<string> DepartmentNames { get; set; }
        #endregion
    }
}
  1. 添加EntityBase.cs文件
namespace API.Domain.Entities
{
    /// <summary>
    /// 实体基类
    /// </summary>
    public class CommonBaseEntity : EntityBase<string>
    {
        /// <summary>
        /// 主键Id
        /// </summary>
        [MaxLength(50)]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        //[ExplicitKey]
        public override string Id { get; set; } = GetId();

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreatedTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 创建人
        /// </summary>
        [MaxLength(50)]
        public string? CreatedBy { get; set; } = default!;
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdatedTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 更新人
        /// </summary>
        [MaxLength(50)]
        public string? UpdatedBy { get; set; } = default!;
        /// <summary>
        /// 是否删除标识0false 1true
        /// </summary>
        public bool IsDeleted { get; set; }
    }

    /// <summary>
    /// EntityBase
    /// </summary>
    /// <typeparam name="TPrimaryKey"></typeparam>
    public abstract class EntityBase<TPrimaryKey>
    {
        /// <summary>
        /// Id
        /// </summary>
        public virtual TPrimaryKey Id { get; set; }
    }
    #region 生成Id
    /// <summary>
    /// 生成Id
    /// </summary>
    /// <param name="size"></param>
    /// <returns></returns>
    private string GetId(string size = "32")
    {
        if (size?.Equals("16") ?? false)
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= b + 1;
            }

            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }
        else if (size?.Equals("32") ?? false)
        {
            return Guid.NewGuid().ToString("N");
        }

        throw new NotSupportedException($"{size} 暂不支持!");
    }
    #endregion
}
  1. 实际使用时候在Service服务中添加类似如下
public class TestService : ITestService
{
  private readonly IMySqlDbRepository<MySqlSjDbContext> _uoRepository;
  public TestService(IMySqlDbRepository<MySqlWebApiDbContext>)
  {
      _uoRepository = uoRepository;
  }
  public string AddUserInfo()
  {
      try
      {
          var transaction = _uoRepository.BeginTransaction();
          var userList = _uoRepository.Get<UserEntity>(a => a.LoginName == "aa").ToList();
          if (userList.Count == 0)
          {
              _uoRepository.Insert(new UserEntity());
              _uoRepository.Insert(new UserEntity());
              result = 1;
          }
          else
          {
              _uoRepository.Rollback();
              return "用户已存在!";
          }
          _uoRepository.Commit();
      }
      catch (Exception e)
      {
          _uoRepository.Rollback();
      }
      return "";
  }
}
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.  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.

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.4 222 4/3/2025
1.0.3 201 3/31/2025
1.0.2 179 7/17/2024
1.0.1 174 5/9/2024
1.0.0 211 10/9/2023
0.0.1 186 4/24/2024