EFCore通用仓储类

using Microsoft.EntityFrameworkCore;
using SmarkParking.Server.IService;
using SmartParking.Server.IEFContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SmarkParking.Server.Service
{
    public class ServiceBase : IServiceBase
    {

        protected DbContext Context { get; private set; }
        public ServiceBase(IEFContext eFContext)
        {
            Context = eFContext.CreateDBContext();
        }

        public void Commit()
        {
            this.Context.SaveChanges(); // 直接保存就行了
        }

        public void Delete<T>(int Id) where T : class
        {
            T t = this.Find<T>(Id);
            if (t == null) throw new Exception("t is null");    // 如果是空就报错
            this.Context.Set<T>().Remove(t);    // 删除这个
            this.Commit();  // 删完提交
        }

        public void Delete<T>(T t) where T : class
        {
            if (t == null) throw new Exception("t is null");    // 如果是空就报错
            this.Context.Set<T>().Attach(t);    // 尊卑删除这个实体
            this.Context.Set<T>().Remove(t);    // 删除这个
            this.Commit();  // 删完提交
        }

        public void Delete<T>(IEnumerable<T> tList) where T : class
        {
            // 将要删除的实体标记上
            foreach (var t in tList)
            {
                this.Context.Set<T>().Attach(t);    // 要删除这个实体
            }
            this.Context.Set<T>().RemoveRange(tList);    // 删除这个集合
            this.Commit();  // 删完提交
        }

        public T Find<T>(int id) where T : class
        {
            return this.Context.Set<T>().Find(id);
        }

        public T Insert<T>(T t) where T : class
        {
            this.Context.Set<T>().Add(t);   // 添加
            this.Commit();  // 提交
            return t;
        }

        public IEnumerable<T> Insert<T>(IEnumerable<T> tList) where T : class
        {
            this.Context.Set<T>().AddRange(tList);  
            this.Commit();  //
            return tList;
        }

        public IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
        {
            return this.Context.Set<T>().Where<T>(funcWhere);
        }

        public void Update<T>(T t) where T : class
        {
            if (t == null) throw new Exception("t is null");

            this.Context.Set<T>().Attach(t);    // 跟踪实体
            this.Context.Entry<T>(t).State = EntityState.Modified;  // 标记为已修改
            this.Commit();
        }

        public void Update<T>(IEnumerable<T> tList) where T : class
        {
            // 跟踪和标记更改
            foreach (var t in tList)
            {
                this.Context.Set<T>().Attach(t);
                this.Context.Entry<T>(t).State = EntityState.Modified;
            }
            this.Commit();
        }

        public virtual void Dispose()
        {
            if (this.Context != null)
                this.Context.Dispose();
        }
    }
}

使用举例子

var users = _loginService.Query<SysUserInfo>(u => u.UserName == username && u.Password == pwd);        // 他是Func<T, bool>类型做委托

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 Entity Framework CoreEF Core)进行数据库操作时,你不需要显式打开或关闭数据库连接。EF Core 会自动管理数据库连接的打开和关闭。 要使用 SQL 更新数据,你可以使用 EF Core 提供的 `ExecuteSqlRaw` 或 `ExecuteSqlInterpolated` 方法。这些方法允许你执行原始的 SQL 查询或命令。 下面是一个示例代码,展示了如何使用 EF Core 执行原始 SQL 更新数据: ```csharp using Microsoft.EntityFrameworkCore; using System; // 创建 DbContext public class YourDbContext : DbContext { public YourDbContext(DbContextOptions<YourDbContext> options) : base(options) { } // DbSet 和其他属性... public DbSet<IPS_Invoice> IPS_Invoices { get; set; } } public class IPS_Invoice { public int IPS_ID { get; set; } public bool BLOCK { get; set; } public DateTime? BLOCKTIME { get; set; } } public class YourRepository { private readonly YourDbContext _dbContext; public YourRepository(YourDbContext dbContext) { _dbContext = dbContext; } public string UpdateBlockTime(IPS_Invoice model) { string message = ""; try { int affectedRows = _dbContext.Database.ExecuteSqlInterpolated($"UPDATE IPS_Invoices SET BLOCK = true, BLOCKTIME = {DateTime.Now} WHERE IPS_ID = {model.IPS_ID}"); if (affectedRows > 0) { message = "True"; } else { message = "No records updated"; } } catch (Exception e) { message = "False"; // 处理异常... } return message; } } ``` 在上述代码中,我们首先创建了一个继承自 `DbContext` 的 `YourDbContext` ,并定义了需要操作的实体 `IPS_Invoice`。然后,我们创建了一个名为 `YourRepository` 的仓储,在该中使用 `ExecuteSqlInterpolated` 方法执行原始的 SQL 更新操作。 在 `UpdateBlockTime` 方法中,我们使用插值字符串(interpolated string)来构建 SQL 命令,并将其传递给 `ExecuteSqlInterpolated` 方法进行执行。如果更新操作成功影响了一行或多行数据,返回的 `affectedRows` 将大于 0。 请根据你的实际情况修改代码,并确保在使用 EF Core 时按照最佳实践进行数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值