First commit
This commit is contained in:
8
DaSaSo.EntityFramework/DaSaSo.EntityFramework.csproj
Normal file
8
DaSaSo.EntityFramework/DaSaSo.EntityFramework.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
19
DaSaSo.EntityFramework/DaSaSoDbContext.cs
Normal file
19
DaSaSo.EntityFramework/DaSaSoDbContext.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using DaSaSo.Domain.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DaSaSo.EntityFramework
|
||||
{
|
||||
public class DaSaSoDbContext : DbContext
|
||||
{
|
||||
public DaSaSoDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<Client> Clients { get; set; }
|
||||
}
|
||||
}
|
||||
20
DaSaSo.EntityFramework/DaSaSoDbContextFactory.cs
Normal file
20
DaSaSo.EntityFramework/DaSaSoDbContextFactory.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DaSaSo.EntityFramework
|
||||
{
|
||||
public class DaSaSoDbContextFactory : IDesignTimeDbContextFactory<DaSaSoDbContext>
|
||||
{
|
||||
public DaSaSoDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<DaSaSoDbContext>();
|
||||
options.UseNpgsql("server = localhost");
|
||||
return new DaSaSoDbContext(options.Options);
|
||||
}
|
||||
}
|
||||
}
|
||||
74
DaSaSo.EntityFramework/Services/GenericDataService.cs
Normal file
74
DaSaSo.EntityFramework/Services/GenericDataService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using DaSaSo.Domain.Model;
|
||||
using DaSaSo.Domain.Service;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DaSaSo.EntityFramework.Services
|
||||
{
|
||||
public class GenericDataService<T> : IDataService<T> where T : DomainObject
|
||||
{
|
||||
private readonly DaSaSoDbContextFactory _contextFactory;
|
||||
|
||||
public GenericDataService(DaSaSoDbContextFactory contextFactory)
|
||||
{
|
||||
this._contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public async Task<T> Create(T entity)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
EntityEntry<T> createdEntity = await context.Set<T>().AddAsync(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return createdEntity.Entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> Delete(int id)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
|
||||
context.Set<T>().Remove(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> Get(int id)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> GetAll()
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
IEnumerable<T> entities = await context.Set<T>().ToListAsync();
|
||||
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> Update(int id, T entity)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
entity.Id = id;
|
||||
await context.Set<T>().Update(entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user