Advertisement
Willcode4cash

Partial dataContext class using Generics

Aug 10th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. namespace MyApp.Web.Models
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Data.Linq;
  6.  
  7.     public partial class MyAppDataContext
  8.     {
  9.         public bool DeleteAllOnSubmit<T>(IEnumerable<T> entities)
  10.         {
  11.             try
  12.             {
  13.                 GetTable(typeof(T)).DeleteAllOnSubmit(entities);
  14.                 SubmitChanges();
  15.                 return true;
  16.             }
  17.             catch (Exception)
  18.             {
  19.                 return false;
  20.             }
  21.         }
  22.  
  23.         public bool DeleteOnSubmit<T>(T entity)
  24.         {
  25.             try
  26.             {
  27.                 GetTable(typeof(T)).DeleteOnSubmit(entity);
  28.                 SubmitChanges();
  29.                 return true;
  30.             }
  31.             catch (Exception)
  32.             {
  33.                 return false;
  34.             }
  35.         }
  36.  
  37.         public bool InsertAllOnSubmit<T>(IEnumerable<T> entities)
  38.         {
  39.             try
  40.             {
  41.                 GetTable(typeof(T)).InsertAllOnSubmit(entities);
  42.                 SubmitChanges();
  43.                 return true;
  44.             }
  45.             catch (Exception)
  46.             {
  47.                 return false;
  48.             }
  49.         }
  50.  
  51.         public bool InsertOnSubmit<T>(ref T entity)
  52.         {
  53.             try
  54.             {
  55.                 GetTable(typeof(T)).InsertOnSubmit(entity);
  56.                 SubmitChanges();
  57.                 return true;
  58.             }
  59.             catch (Exception)
  60.             {
  61.                 return false;
  62.             }
  63.         }
  64.         partial void OnCreated()
  65.         {
  66.             var options = new DataLoadOptions();
  67.             // Add loadwith elements
  68.             // options.LoadWith<parentClass>(p => p.columnName);
  69.             LoadOptions = options;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement