MVC - Entity Framework using Connection Extension (Insert,update,selet,Delete)
MVC - Entity Framework using Connection Extension (Insert,update,selet,Delete)
DB Context
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using API.Models;
namespace API.Database
{
public class APIDbContext: DbContext
{
public APIDbContext()
: base("APIDbContext")
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public override int SaveChanges()
{
try
{
return base.SaveChanges();
}
catch (Exception e)
{
//Debug.WriteLine(e.Message);
throw;
}
}
public DbSet<User> Users { get; set; }
}
}
Connection Extension:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Data.Entity;
using API;
namespace API.Database
{
public static class ConnectionExtensions
{
//Save
public static TEntity Save<TEntity>(this APIDbContextcontext, TEntity entity) where TEntity : class
{
try
{
context.Entry(entity).State = System.Data.Entity.EntityState.Added;
context.SaveChanges();
} catch(Exception ex)
{
Console.WriteLine(ex.Message);
entity = null;
}
return entity;
}
//Update
public static TEntity Update<TEntity>(this APIDbContextcontext, TEntity entity) where TEntity : class
{
try
{
context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
entity = null;
}
return entity;
}
//Delete
public static TEntity Delete<TEntity>(this APIDbContextcontext, TEntity entity) where TEntity : class
{
try
{
context.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
entity = null;
}
return entity;
}
//DeleteById
public static TEntity DeleteById<TEntity>(this APIDbContextcontext, long id) where TEntity : class, IDataModel
{
TEntity entity;
try
{
var q = from o in context.Set<TEntity>()
where o.id == id
select o;
entity = q.SingleOrDefault();
context.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return entity;
}
//Get
public static TEntity Get<TEntity>(this APIDbContextcontext, long id) where TEntity : class, IDataModel
{
TEntity entity;
try
{
var q = from o in context.Set<TEntity>()
where o.id == id
select o;
entity = q.SingleOrDefault();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return entity;
}
//GetBy(this APIDbContextcontext, Expression<Func<TEntity, object>> field, string value)
public static TEntity GetBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, object>> field, string value) where TEntity : class, IDataModel
{
IQueryable<TEntity> query = context.Set<TEntity>();
query.Include<TEntity, object>(field);
TEntity t = query.SingleOrDefault<TEntity>();
return t;
}
//GetBy(this APIDbContextcontext, Expression<Func<TEntity, object>> field, long value)
public static TEntity GetBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, object>> field, long value) where TEntity : class, IDataModel
{
IQueryable<TEntity> query = context.Set<TEntity>();
query.Include<TEntity, object>(field);
TEntity t = query.SingleOrDefault<TEntity>();
return t;
}
//GetBy(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr)
public static TEntity GetBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr) where TEntity : class, IDataModel
{
TEntity t;
try
{
IQueryable<TEntity> query = context.Set<TEntity>();
t = query.Where<TEntity>(expr).SingleOrDefault<TEntity>();
} catch (Exception ex)
{
t = null;
}
return t;
}
//FetchBy
public static IEnumerable<TEntity> FetchBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, object>> field, object value) where TEntity : class, IDataModel
{
IQueryable<TEntity> query = context.Set<TEntity>();
query.Include<TEntity, object>(field);
return query.ToList<TEntity>();
}
//FetchBy(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr)
public static IEnumerable<TEntity> FetchBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr) where TEntity : class, IDataModel
{
IQueryable<TEntity> query = context.Set<TEntity>();
IEnumerable<TEntity> t = query.Where<TEntity>(expr).ToList<TEntity>();
return t;
}
}
}
No comments:
Post a Comment