initial
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Mf.Core\Mf.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,326 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Mf.Core.Interfaces;
|
||||
using Mf.Core.SubEntities;
|
||||
using Mf.Core.SubEntities.QueryModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Mf.EfCore.Services
|
||||
{
|
||||
public class EntityFrameworkCoreServices<TEntity> : IGenericService<TEntity>, IDisposable
|
||||
where TEntity : class, IEntity
|
||||
{
|
||||
protected readonly DbContext DbContext;
|
||||
public DbSet<TEntity> Entities { get; }
|
||||
public virtual IQueryable<TEntity> Table => Entities;
|
||||
public virtual IQueryable<TEntity> TableNoTracking => Entities.AsNoTracking();
|
||||
|
||||
|
||||
public EntityFrameworkCoreServices(DbContext dbContext)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
Entities = DbContext.Set<TEntity>(); // City => Cities
|
||||
}
|
||||
|
||||
#region Async Method
|
||||
public virtual async Task<TEntity> GetByIdAsync(CancellationToken cancellationToken, params object[] ids)
|
||||
{
|
||||
return await Entities.FindAsync(ids, cancellationToken).AsTask();
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
var entityFull = await DbContext.AddAsync(entity, cancellationToken).ConfigureAwait(false);
|
||||
if (saveNow)
|
||||
{
|
||||
try
|
||||
{
|
||||
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
entityFull.State = EntityState.Detached;
|
||||
}
|
||||
}
|
||||
return entityFull.Entity;
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
await Entities.AddRangeAsync(entities, cancellationToken).ConfigureAwait(false);
|
||||
if (saveNow)
|
||||
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return entities;
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
var EntityState = Entities.Update(entity);
|
||||
if (saveNow)
|
||||
{
|
||||
await DbContext.SaveChangesAsync(cancellationToken);
|
||||
EntityState.State = Microsoft.EntityFrameworkCore.EntityState.Detached;
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntity>> UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
Entities.UpdateRange(entities);
|
||||
if (saveNow)
|
||||
await DbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> DeleteAsync(TEntity entity, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
Entities.Remove(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
if (saveNow)
|
||||
await DbContext.SaveChangesAsync(cancellationToken);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntity>> DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
Entities.RemoveRange(entities);
|
||||
if (saveNow)
|
||||
await DbContext.SaveChangesAsync(cancellationToken);
|
||||
return entities;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Sync Methods
|
||||
public virtual TEntity GetById(params object[] ids)
|
||||
{
|
||||
return Entities.Find(ids);
|
||||
}
|
||||
|
||||
public virtual void Add(TEntity entity, bool saveNow = true)
|
||||
{
|
||||
|
||||
Entities.Add(entity);
|
||||
if (saveNow)
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual void AddRange(IEnumerable<TEntity> entities, bool saveNow = true)
|
||||
{
|
||||
|
||||
Entities.AddRange(entities);
|
||||
if (saveNow)
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual void Update(TEntity entity, bool saveNow = true)
|
||||
{
|
||||
|
||||
Entities.Update(entity);
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual void UpdateRange(IEnumerable<TEntity> entities, bool saveNow = true)
|
||||
{
|
||||
|
||||
Entities.UpdateRange(entities);
|
||||
if (saveNow)
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual void Delete(TEntity entity, bool saveNow = true)
|
||||
{
|
||||
|
||||
Entities.Remove(entity);
|
||||
if (saveNow)
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual void DeleteRange(IEnumerable<TEntity> entities, bool saveNow = true)
|
||||
{
|
||||
|
||||
Entities.RemoveRange(entities);
|
||||
if (saveNow)
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Attach & Detach
|
||||
public virtual void Detach(TEntity entity)
|
||||
{
|
||||
|
||||
var entry = DbContext.Entry(entity);
|
||||
if (entry != null)
|
||||
entry.State = EntityState.Detached;
|
||||
}
|
||||
|
||||
public virtual void Attach(TEntity entity)
|
||||
{
|
||||
|
||||
if (DbContext.Entry(entity).State == EntityState.Detached)
|
||||
Entities.Attach(entity);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Explicit Loading
|
||||
public virtual async Task LoadCollectionAsync<TProperty>(TEntity entity, Expression<Func<TEntity, IEnumerable<TProperty>>> collectionProperty, CancellationToken cancellationToken)
|
||||
where TProperty : class
|
||||
{
|
||||
Attach(entity);
|
||||
|
||||
var collection = DbContext.Entry(entity).Collection(collectionProperty);
|
||||
if (!collection.IsLoaded)
|
||||
await collection.LoadAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual void LoadCollection<TProperty>(TEntity entity, Expression<Func<TEntity, IEnumerable<TProperty>>> collectionProperty)
|
||||
where TProperty : class
|
||||
{
|
||||
Attach(entity);
|
||||
var collection = DbContext.Entry(entity).Collection(collectionProperty);
|
||||
if (!collection.IsLoaded)
|
||||
collection.Load();
|
||||
}
|
||||
|
||||
public virtual async Task LoadReferenceAsync<TProperty>(TEntity entity, Expression<Func<TEntity, TProperty>> referenceProperty, CancellationToken cancellationToken)
|
||||
where TProperty : class
|
||||
{
|
||||
Attach(entity);
|
||||
var reference = DbContext.Entry(entity).Reference(referenceProperty);
|
||||
if (!reference.IsLoaded)
|
||||
await reference.LoadAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public virtual void LoadReference<TProperty>(TEntity entity, Expression<Func<TEntity, TProperty>> referenceProperty)
|
||||
where TProperty : class
|
||||
{
|
||||
Attach(entity);
|
||||
var reference = DbContext.Entry(entity).Reference(referenceProperty);
|
||||
if (!reference.IsLoaded)
|
||||
reference.Load();
|
||||
}
|
||||
|
||||
public void SaveChange()
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public async Task<bool> SaveChangeAsync()
|
||||
{
|
||||
await DbContext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
public void Dispose()
|
||||
{
|
||||
DbContext.Dispose();
|
||||
}
|
||||
|
||||
|
||||
//TODO: Complte there
|
||||
public IEnumerable<TEntity> GetAll()
|
||||
{
|
||||
return TableNoTracking;
|
||||
}
|
||||
|
||||
async Task<IEntity> IGenericService<TEntity>.GetByIdAsync(CancellationToken cancellationToken, params object[] ids)
|
||||
{
|
||||
var res = await GetByIdAsync(cancellationToken, ids);
|
||||
return (IEntity)res;
|
||||
}
|
||||
|
||||
public Task<IEnumerable<IEntity>> AddRangeAsync(IEnumerable<IEntity> entities, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<IEntity> UpdateAsync(object entity, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
var en = (TEntity)entity;
|
||||
var result = await UpdateAsync(en, cancellationToken, saveNow);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IEntity> AddAsync(object entity, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
var b = (TEntity)entity;
|
||||
var res = await AddAsync(b, cancellationToken, saveNow);
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<IEntity> DeleteAsync(object entity, CancellationToken cancellationToken, bool saveNow = true)
|
||||
{
|
||||
var en = (TEntity)entity;
|
||||
var result = await DeleteAsync(en, cancellationToken, saveNow);
|
||||
return result;
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> GetList(QueryRequest? query = null)
|
||||
{
|
||||
var products = TableNoTracking.AsQueryable();
|
||||
if (query == null)
|
||||
{
|
||||
return products;
|
||||
}
|
||||
foreach (var include in query.Includes)
|
||||
{
|
||||
products = products.Include(include);
|
||||
}
|
||||
// 2. Apply Filters
|
||||
foreach (var filter in query.Filters)
|
||||
{
|
||||
products = ApplyFilter(products, filter);
|
||||
}
|
||||
// 3. Apply Skip and Take
|
||||
if (query.Skip.HasValue)
|
||||
products = products.Skip(query.Skip.Value);
|
||||
if (query.Take.HasValue)
|
||||
products = products.Take(query.Take.Value);
|
||||
return products;
|
||||
}
|
||||
|
||||
private IQueryable<TEntity> ApplyFilter(IQueryable<TEntity> query, Filter filter)
|
||||
{
|
||||
var parameter = Expression.Parameter(typeof(TEntity), "x");
|
||||
var property = Expression.Property(parameter, filter.Field);
|
||||
var constant = Expression.Constant(Convert.ChangeType(filter.Value, property.Type));
|
||||
|
||||
Expression body = filter.Operator switch
|
||||
{
|
||||
"=" => Expression.Equal(property, constant),
|
||||
">" => Expression.GreaterThan(property, constant),
|
||||
">=" => Expression.GreaterThanOrEqual(property, constant),
|
||||
"<" => Expression.LessThan(property, constant),
|
||||
"<=" => Expression.LessThanOrEqual(property, constant),
|
||||
"!=" => Expression.NotEqual(property, constant),
|
||||
"Contains" => Expression.Call(property, nameof(string.Contains), Type.EmptyTypes, constant),
|
||||
"StartsWith" => Expression.Call(property, nameof(string.Contains), Type.EmptyTypes, constant),
|
||||
"EndsWith" => Expression.Call(property, nameof(string.Contains), Type.EmptyTypes, constant),
|
||||
_ => throw new NotSupportedException($"Operator {filter.Operator} not supported.")
|
||||
};
|
||||
|
||||
var lambda = Expression.Lambda<Func<TEntity, bool>>(body, parameter);
|
||||
return query.Where(lambda);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Mf.Core.Interfaces;
|
||||
using Mf.Core.SubEntities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Mf.EfCore.Services
|
||||
{
|
||||
public class PostgresqlContextCore : DbContext, IContextCore
|
||||
{
|
||||
public PostgresqlContextCore(DbContextOptions optinos) : base(optinos) { }
|
||||
|
||||
public List<Type>? Entities { get; set; }
|
||||
|
||||
public IGenericService<TypeModel> GetService<TypeModel>() where TypeModel : class, IEntity
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(typeof(TypeModel));
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqlServerContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this });
|
||||
return (EntityFrameworkCoreServices<TypeModel>)telBotServiceInstance;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
|
||||
return (EntityFrameworkCoreServices<TypeModel>)telBotServiceInstance!;
|
||||
}
|
||||
}
|
||||
|
||||
public object GetService(Type type)
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(type);
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqlServerContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this });
|
||||
return telBotServiceInstance;
|
||||
}
|
||||
else
|
||||
{
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
return telBotServiceInstance!;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<object> GetGenericServices(List<Type> entities)
|
||||
{
|
||||
List<object> services = new List<object>();
|
||||
foreach (var type in entities)
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(type);
|
||||
// Get the constructor of the TelBot_Service<T> type
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqlServerContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
// If constructor exists, create an instance with a parameter
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this }); // Provide the parameter value here
|
||||
//var ssss = (IGenericService<IEntity>)telBotServiceInstance;
|
||||
//var sss = (EntityFrameworkCoreServices<IEntity>)telBotServiceInstance;
|
||||
services.Add(telBotServiceInstance);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If no constructor exists, create an instance without parameters
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
//var sss = (EntityFrameworkCoreServices<IEntity>)telBotServiceInstance;
|
||||
services.Add(telBotServiceInstance);
|
||||
}
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
if (Entities != null)
|
||||
{
|
||||
foreach (var s in Entities)
|
||||
{
|
||||
var entity = modelBuilder.Entity(s);
|
||||
}
|
||||
|
||||
|
||||
//for postgres db
|
||||
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
||||
{
|
||||
foreach (var property in entityType.GetProperties())
|
||||
{
|
||||
if (property.ClrType == typeof(DateTime))
|
||||
{
|
||||
property.SetValueConverter(new ValueConverter<DateTime, DateTime>(
|
||||
v => v.Kind == DateTimeKind.Utc ? v : v.ToUniversalTime(), // ذخیره
|
||||
v => DateTime.SpecifyKind(v, DateTimeKind.Utc) // بازیابی
|
||||
));
|
||||
}
|
||||
|
||||
else if (property.ClrType == typeof(DateTime?))
|
||||
{
|
||||
property.SetValueConverter(new ValueConverter<DateTime?, DateTime?>(
|
||||
v => v.HasValue ? (v.Value.Kind == DateTimeKind.Utc ? v : v.Value.ToUniversalTime()) : v, // ذخیره
|
||||
v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : v // بازیابی
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void IContextCore.Dispose()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
|
||||
public void Migrate()
|
||||
{
|
||||
this.Migrate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Mf.Core.Interfaces;
|
||||
using Mf.Core.SubEntities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Mf.EfCore.Services
|
||||
{
|
||||
public class SqlServerContextCore : DbContext, IContextCore
|
||||
{
|
||||
public SqlServerContextCore(DbContextOptions optinos) : base(optinos) { }
|
||||
|
||||
public List<Type>? Entities { get; set; }
|
||||
|
||||
public IGenericService<TypeModel> GetService<TypeModel>() where TypeModel : class, IEntity
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(typeof(TypeModel));
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqlServerContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this });
|
||||
return (EntityFrameworkCoreServices<TypeModel>)telBotServiceInstance;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
|
||||
return (EntityFrameworkCoreServices<TypeModel>)telBotServiceInstance!;
|
||||
}
|
||||
}
|
||||
|
||||
public object GetService(Type type)
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(type);
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqlServerContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this });
|
||||
return telBotServiceInstance;
|
||||
}
|
||||
else
|
||||
{
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
return telBotServiceInstance!;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<object> GetGenericServices(List<Type> entities)
|
||||
{
|
||||
List<object> services = new List<object>();
|
||||
foreach (var type in entities)
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(type);
|
||||
// Get the constructor of the TelBot_Service<T> type
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqlServerContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
// If constructor exists, create an instance with a parameter
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this }); // Provide the parameter value here
|
||||
//var ssss = (IGenericService<IEntity>)telBotServiceInstance;
|
||||
//var sss = (EntityFrameworkCoreServices<IEntity>)telBotServiceInstance;
|
||||
services.Add(telBotServiceInstance);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If no constructor exists, create an instance without parameters
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
//var sss = (EntityFrameworkCoreServices<IEntity>)telBotServiceInstance;
|
||||
services.Add(telBotServiceInstance);
|
||||
}
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
if (Entities != null)
|
||||
{
|
||||
foreach (var s in Entities)
|
||||
{
|
||||
var entity = modelBuilder.Entity(s);
|
||||
}
|
||||
|
||||
|
||||
//for postgres db
|
||||
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
||||
{
|
||||
foreach (var property in entityType.GetProperties())
|
||||
{
|
||||
if (property.ClrType == typeof(DateTime))
|
||||
{
|
||||
property.SetValueConverter(new ValueConverter<DateTime, DateTime>(
|
||||
v => v.Kind == DateTimeKind.Utc ? v : v.ToUniversalTime(), // ذخیره
|
||||
v => DateTime.SpecifyKind(v, DateTimeKind.Utc) // بازیابی
|
||||
));
|
||||
}
|
||||
|
||||
else if (property.ClrType == typeof(DateTime?))
|
||||
{
|
||||
property.SetValueConverter(new ValueConverter<DateTime?, DateTime?>(
|
||||
v => v.HasValue ? (v.Value.Kind == DateTimeKind.Utc ? v : v.Value.ToUniversalTime()) : v, // ذخیره
|
||||
v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : v // بازیابی
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void IContextCore.Dispose()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
|
||||
public void Migrate()
|
||||
{
|
||||
this.Migrate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Mf.Core.Interfaces;
|
||||
using Mf.Core.SubEntities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Mf.EfCore.Services
|
||||
{
|
||||
public abstract class SqliteContextCore : DbContext, IContextCore
|
||||
{
|
||||
public string DbName { get; set; }
|
||||
public string DbPath { get; set; }
|
||||
|
||||
public List<Type> Entities { get; set; }
|
||||
|
||||
public void SetDefaultConfig()
|
||||
{
|
||||
DbName = "defualt-database-name.db";
|
||||
DbPath = "";
|
||||
}
|
||||
public SqliteContextCore(bool setDefaultConfig = true)
|
||||
{
|
||||
|
||||
if (setDefaultConfig)
|
||||
{
|
||||
SetDefaultConfig();
|
||||
CreateDB();
|
||||
}
|
||||
|
||||
}
|
||||
public void CreateDB()
|
||||
{
|
||||
//TODO clean this
|
||||
//var strDestination = "copy.db";
|
||||
//string dir = Directory.GetCurrentDirectory();
|
||||
//dir = Path.Combine(dir, strDestination);
|
||||
|
||||
|
||||
//string orgDir = Path.Combine(Directory.GetCurrentDirectory(), DbPath, DbName);
|
||||
|
||||
//if (!Path.Exists(Path.Combine(Directory.GetCurrentDirectory(), DbPath)))
|
||||
//{
|
||||
// Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), DbPath));
|
||||
//}
|
||||
|
||||
//if (!File.Exists(orgDir))
|
||||
//{
|
||||
|
||||
// System.Data.SQLite.SQLiteConnection.CreateFile(orgDir);
|
||||
//}
|
||||
|
||||
//if (!File.Exists(dir))
|
||||
//{
|
||||
|
||||
// System.Data.SQLite.SQLiteConnection.CreateFile(dir);
|
||||
//}
|
||||
|
||||
//using (var location = new System.Data.SQLite.SQLiteConnection(string.Format(@"Data Source={0};", orgDir)))
|
||||
////using (var destination = new System.Data.SQLite.SQLiteConnection(string.Format(@"Data Source={0}", dir)))
|
||||
//{
|
||||
// location.Open();
|
||||
// //destination.Open();
|
||||
// //location.BackupDatabase(destination, "main", "main", -1, null, 0);
|
||||
// //destination.Close();
|
||||
//}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseSqlite($"Data Source={DbPath}{DbName}");
|
||||
}
|
||||
|
||||
|
||||
public IGenericService<TypeModel> GetService<TypeModel>() where TypeModel : class, IEntity
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(typeof(TypeModel));
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqliteContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this });
|
||||
return (EntityFrameworkCoreServices<TypeModel>)telBotServiceInstance;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
|
||||
return (EntityFrameworkCoreServices<TypeModel>)telBotServiceInstance!;
|
||||
}
|
||||
}
|
||||
|
||||
public object GetService(Type type)
|
||||
{
|
||||
var telBotServiceType = typeof(EntityFrameworkCoreServices<>).MakeGenericType(type);
|
||||
var constructor = telBotServiceType.GetConstructor(new[] { typeof(SqliteContextCore) });
|
||||
if (constructor != null)
|
||||
{
|
||||
var telBotServiceInstance = constructor.Invoke(new object[] { this });
|
||||
return telBotServiceInstance;
|
||||
}
|
||||
else
|
||||
{
|
||||
var telBotServiceInstance = Activator.CreateInstance(telBotServiceType);
|
||||
return telBotServiceInstance!;
|
||||
}
|
||||
}
|
||||
|
||||
void IContextCore.Dispose()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
|
||||
public void Migrate()
|
||||
{
|
||||
try
|
||||
{
|
||||
this.Migrate();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
if (Entities != null)
|
||||
{
|
||||
foreach (var s in Entities)
|
||||
{
|
||||
var entity = modelBuilder.Entity(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user