Objekte können hinzugefügt werdne
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using KanSan.Base.Interfaces;
|
||||
using KanSan.Base.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -19,6 +20,7 @@ namespace KanSan.Base
|
||||
this.context = context;
|
||||
if (context == null) throw new ArgumentNullException("context");
|
||||
this.dbSet = context.Set<TEntity>();
|
||||
|
||||
}
|
||||
public virtual void Delete(TEntity entityToDelete)
|
||||
{
|
||||
@@ -33,9 +35,15 @@ namespace KanSan.Base
|
||||
Delete(entityToDelete);
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> GetAll(string include)
|
||||
{
|
||||
return dbSet.Include(include);
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
|
||||
{
|
||||
IQueryable<TEntity> query = dbSet;
|
||||
|
||||
|
||||
if (filter != null)
|
||||
{
|
||||
@@ -72,9 +80,30 @@ namespace KanSan.Base
|
||||
dbSet.Add(entity);
|
||||
}
|
||||
|
||||
public void Update(TEntity entityToUpdate)
|
||||
public IQueryable<TEntity> Include(params Expression<Func<TEntity,object>>[] includeExpressions)
|
||||
{
|
||||
dbSet.Attach(entityToUpdate);
|
||||
IQueryable<TEntity> query = null;
|
||||
foreach(var include in includeExpressions)
|
||||
{
|
||||
query = dbSet.Include(include);
|
||||
}
|
||||
return query ?? dbSet;
|
||||
}
|
||||
|
||||
public void Update(TEntity entityToUpdate, bool attach = true)
|
||||
{
|
||||
if (!attach)
|
||||
{
|
||||
var d = context.Entry(entityToUpdate);
|
||||
if (d != null)
|
||||
{
|
||||
d.State = EntityState.Detached;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dbSet.Attach(entityToUpdate);
|
||||
}
|
||||
IDatabaseEntry x = (entityToUpdate as IDatabaseEntry);
|
||||
if(x == null)
|
||||
return;
|
||||
|
||||
@@ -6,16 +6,18 @@ using System.Text;
|
||||
|
||||
namespace KanSan.Base.Interfaces
|
||||
{
|
||||
public interface IRepository<TEntity> where TEntity: class
|
||||
public interface IRepository<TEntity> where TEntity : class
|
||||
{
|
||||
void Delete(TEntity entityToDelete);
|
||||
void Delete(object id);
|
||||
IEnumerable<TEntity> GetAll(string include);
|
||||
IEnumerable<TEntity> Get(
|
||||
Expression<Func<TEntity, bool>> filter = null,
|
||||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
|
||||
string includeProperties = "");
|
||||
TEntity GetByID(object id);
|
||||
void Insert(TEntity entity);
|
||||
void Update(TEntity entityToUpdate);
|
||||
void Update(TEntity entityToUpdate, bool attach = true);
|
||||
IQueryable<TEntity> Include(params Expression<Func<TEntity, object>>[] includes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace KanSan.Base
|
||||
public UnitOfWork(KanSanContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
//_dbContext.ChangeTracker.QueryTrackingBehavior = Microsoft.EntityFrameworkCore.QueryTrackingBehavior.NoTracking;
|
||||
}
|
||||
|
||||
public IRepository<Projekt> ProjekteRepository => _projekte ?? (_projekte = new BaseRepository<Projekt>(_dbContext));
|
||||
|
||||
Reference in New Issue
Block a user