using System;
|
using System.Collections.Generic;
|
using System.Data.Entity;
|
using System.Data.Entity.Validation;
|
using System.Linq;
|
|
namespace PalGain.Core
|
{
|
/// <summary>
|
/// Entity Framework repository
|
/// </summary>
|
public partial class EfRepository<T> : IRespository<T> where T : BaseEntity
|
{
|
#region Fields
|
|
private readonly IDbContext _context;
|
private IDbSet<T> _entities;
|
|
#endregion
|
|
#region Ctor
|
|
/// <summary>
|
/// Ctor
|
/// </summary>
|
/// <param name="context">Object context</param>
|
public EfRepository(IDbContext context)
|
{
|
this._context = context;
|
}
|
|
#endregion
|
|
#region Methods
|
|
/// <summary>
|
/// Get entity by identifier
|
/// </summary>
|
/// <param name="id">Identifier</param>
|
/// <returns>Entity</returns>
|
public virtual T GetById(object id)
|
{
|
//see some suggested performance optimization (not tested)
|
//http://stackoverflow.com/questions/11686225/dbset-find-method-ridiculously-slow-compared-to-singleordefault-on-id/11688189#comment34876113_11688189
|
return this.Entities.Find(id);
|
}
|
/// <summary>
|
/// Ö´ÐÐSQLÓï¾ä
|
/// </summary>
|
/// <returns></returns>
|
public virtual int ExecuteSqlCommand(string sql)
|
{
|
return this._context.ExecuteSqlCommand(sql);
|
}
|
|
/// <summary>
|
/// Insert entity
|
/// </summary>
|
/// <param name="entity">Entity</param>
|
public virtual void Insert(T entity)
|
{
|
try
|
{
|
if (entity == null)
|
throw new ArgumentNullException("entity");
|
|
this.Entities.Add(entity);
|
|
this._context.SaveChanges();
|
}
|
catch (DbEntityValidationException dbEx)
|
{
|
var msg = string.Empty;
|
|
foreach (var validationErrors in dbEx.EntityValidationErrors)
|
foreach (var validationError in validationErrors.ValidationErrors)
|
msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
|
|
var fail = new Exception(msg, dbEx);
|
throw fail;
|
}
|
}
|
|
/// <summary>
|
/// Insert entities
|
/// </summary>
|
/// <param name="entities">Entities</param>
|
public virtual void Insert(IEnumerable<T> entities)
|
{
|
try
|
{
|
if (entities == null)
|
throw new ArgumentNullException("entities");
|
|
foreach (var entity in entities)
|
this.Entities.Add(entity);
|
|
this._context.SaveChanges();
|
}
|
catch (DbEntityValidationException dbEx)
|
{
|
var msg = string.Empty;
|
|
foreach (var validationErrors in dbEx.EntityValidationErrors)
|
foreach (var validationError in validationErrors.ValidationErrors)
|
msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
|
|
var fail = new Exception(msg, dbEx);
|
throw fail;
|
}
|
}
|
|
/// <summary>
|
/// Update entity
|
/// </summary>
|
/// <param name="entity">Entity</param>
|
public virtual void Update(T entity)
|
{
|
try
|
{
|
if (entity == null)
|
throw new ArgumentNullException("entity");
|
|
int ret = this._context.SaveChanges();
|
BatchService.Framework.Utility.LogHelper.Info("¸üвÙ×÷Ö´Ðнá¹û:" + ret + ",Id:" + entity.Id);
|
}
|
catch (DbEntityValidationException dbEx)
|
{
|
var msg = string.Empty;
|
|
foreach (var validationErrors in dbEx.EntityValidationErrors)
|
foreach (var validationError in validationErrors.ValidationErrors)
|
msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
|
|
var fail = new Exception(msg, dbEx);
|
throw fail;
|
}
|
}
|
|
/// <summary>
|
/// Update entities
|
/// </summary>
|
/// <param name="entities">Entities</param>
|
public virtual void Update(IEnumerable<T> entities)
|
{
|
try
|
{
|
if (entities == null)
|
throw new ArgumentNullException("entities");
|
|
this._context.SaveChanges();
|
}
|
catch (DbEntityValidationException dbEx)
|
{
|
var msg = string.Empty;
|
|
foreach (var validationErrors in dbEx.EntityValidationErrors)
|
foreach (var validationError in validationErrors.ValidationErrors)
|
msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
|
|
var fail = new Exception(msg, dbEx);
|
throw fail;
|
}
|
}
|
|
/// <summary>
|
/// Delete entity
|
/// </summary>
|
/// <param name="entity">Entity</param>
|
public virtual void Delete(T entity)
|
{
|
try
|
{
|
if (entity == null)
|
throw new ArgumentNullException("entity");
|
|
this.Entities.Remove(entity);
|
|
this._context.SaveChanges();
|
}
|
catch (DbEntityValidationException dbEx)
|
{
|
var msg = string.Empty;
|
|
foreach (var validationErrors in dbEx.EntityValidationErrors)
|
foreach (var validationError in validationErrors.ValidationErrors)
|
msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
|
|
var fail = new Exception(msg, dbEx);
|
throw fail;
|
}
|
}
|
|
/// <summary>
|
/// Delete entities
|
/// </summary>
|
/// <param name="entities">Entities</param>
|
public virtual void Delete(IEnumerable<T> entities)
|
{
|
try
|
{
|
if (entities == null)
|
throw new ArgumentNullException("entities");
|
|
foreach (var entity in entities)
|
this.Entities.Remove(entity);
|
|
this._context.SaveChanges();
|
}
|
catch (DbEntityValidationException dbEx)
|
{
|
var msg = string.Empty;
|
|
foreach (var validationErrors in dbEx.EntityValidationErrors)
|
foreach (var validationError in validationErrors.ValidationErrors)
|
msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
|
|
var fail = new Exception(msg, dbEx);
|
throw fail;
|
}
|
}
|
|
#endregion
|
|
#region Properties
|
|
/// <summary>
|
/// Gets a table
|
/// </summary>
|
public virtual IQueryable<T> Table
|
{
|
get
|
{
|
|
return this.Entities;
|
}
|
}
|
|
/// <summary>
|
/// Gets a table with "no tracking" enabled (EF feature) Use it only when you load record(s) only for read-only operations
|
/// </summary>
|
public virtual IQueryable<T> TableNoTracking
|
{
|
get
|
{
|
return this.Entities.AsNoTracking();
|
}
|
}
|
|
/// <summary>
|
/// Entities
|
/// </summary>
|
protected virtual IDbSet<T> Entities
|
{
|
get
|
{
|
if (_entities == null)
|
_entities = _context.Set<T>();
|
return _entities;
|
}
|
}
|
|
#endregion
|
}
|
}
|