12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using System;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- using Luticate2.Utils.Dbo;
- using Luticate2.Utils.Dbo.Result;
- using Microsoft.EntityFrameworkCore;
-
- namespace Luticate2.Utils.DataAccess
- {
- public abstract class LuEfDataAccess<TModel, TDbContext> : LuDataAccess
- where TModel : class
- where TDbContext : DbContext
- {
- protected readonly TDbContext Db;
-
- protected readonly DbSet<TModel> Table;
-
- protected LuEfDataAccess(TDbContext db, DbSet<TModel> table)
- {
- Db = db;
- Table = table;
- }
-
- public LuResult<T> Execute<T>(Func<LuResult<T>> func)
- {
- try
- {
- return func();
- }
- catch (Exception e)
- {
- return LuResult<T>.Error(LuStatus.DbError, e);
- }
- }
-
- public static Expression<Func<TModel, bool>> GetExpression(params KeyValuePair<string, object>[] keys)
- {
- var param = Expression.Parameter(typeof(TModel), "x");
- Expression totalExp = null;
- foreach (var pair in keys)
- {
- var equalExp = Expression.Equal(Expression.Property(param, pair.Key), Expression.Constant(pair.Value));
- totalExp = totalExp == null ? equalExp : Expression.And(equalExp, totalExp);
- }
- return Expression.Lambda<Func<TModel, bool>>(totalExp, param);
- }
-
- }
- }
|