COA.EnterpriseServices/COA.EnterpriseServices.DataAccess.EntityFramework/EntityDataAccess.cs

59 lines
1.4 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using System;
2020-10-14 00:56:53 +00:00
using System.Collections.Generic;
2020-10-14 01:24:27 +00:00
using System.Linq;
2020-10-14 00:56:53 +00:00
using System.Linq.Expressions;
namespace COA.EnterpriseServices.DataAccess.EntityFramework
{
public class EntityDataAccess<T> : IDataAccess<T> where T : class, IRecord, new()
2020-10-14 00:56:53 +00:00
{
public bool Add(T item)
{
2020-10-14 01:24:27 +00:00
using (var context = new QuickBaseContext())
{
context.Add(item);
context.SaveChanges();
}
return true;
2020-10-14 00:56:53 +00:00
}
public bool Update(T item)
{
2020-10-14 01:24:27 +00:00
using (var context = new QuickBaseContext())
{
var entry = context.Entry(item);
entry.State = EntityState.Modified;
context.SaveChanges();
}
return true;
}
2020-10-14 00:56:53 +00:00
public ICollection<T> Get(Expression<Func<T, bool>> query)
{
2020-10-14 01:24:27 +00:00
using (var context = new QuickBaseContext())
{
return context
.Set<T>()
.Where(query)
.ToList();
}
2020-10-14 00:56:53 +00:00
}
public ICollection<T> Raw(string command, params object[] parameters)
{
using (var context = new QuickBaseContext())
{
return context.Set<T>()
.FromSqlRaw(command, parameters)
.ToList();
}
}
2020-10-14 00:56:53 +00:00
}
}