WPF hinzugefügt
This commit is contained in:
55
DataStoring.EfCore/Repository.cs
Normal file
55
DataStoring.EfCore/Repository.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using DataStoring.Contract;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DataStoring.EfCore
|
||||
{
|
||||
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
|
||||
{
|
||||
private readonly DPContext _db;
|
||||
|
||||
public IQueryable<TEntity> Query => _db.Set<TEntity>();
|
||||
|
||||
public Repository(DPContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public void Delete(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var entity = _db.Set<TEntity>().Find(id);
|
||||
if(entity == null)
|
||||
{
|
||||
throw new Exception("Id not found");
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw new Exception("Cant delete entity with id " + id);
|
||||
}
|
||||
}
|
||||
|
||||
public void Insert(TEntity entity)
|
||||
{
|
||||
_db.Set<TEntity>().Add(entity);
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
public void Update(TEntity entity)
|
||||
{
|
||||
_db.Entry(entity).State = EntityState.Modified;
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
|
||||
{
|
||||
IQueryable<TEntity> query = _db.Set<TEntity>();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user