This project has moved. For the latest updates, please go here.

LinqToSql

Helper methods for use with Linq-to-SQL tables
DEPRECATED

Get<>()

T Get<T>(this DataContext dataContext, object primaryKey)
Details:
  • Returns an object of Type T from the supplied dataContext with it's Primary Key equal to primaryKey, primaryKey can also be a Dictionary<string, object> for composite keys.
  • T : class, INotifyPropertyChanged
  • T must be a table member of DataContext
  • T must have a CustomAttribute of Type ColumnAttribute that .IsPrimaryKey == true and .DbType includes NOT NULL
  • Throws a details ArgumentException if T's Primary Key Type and primaryKey's type do not match
Usage:
//initialize the DataContext
StoreDataContext db = new StoreDataContext();

//gets a Product with Primary Key 5 from the Products Table
Product product =  db.Get<Product>(5);

//gets an Employee from the Employees table, this table has a Guid for the Primary Key
Guid stevesId = new Guid("4fcc0b82-b137-4e4b-935e-872ed662ba53");
Employee steve = db.Get<Employee>(stevesId);

//notice that it works for all types of Primary Keys

GetPrimaryKey()

IPrimaryKey GetPrimaryKey<T>()
Details:
  • Gets the PropertyInfo(s) of the supplied object T
  • T : class, INotifyPropertyChanged
  • At least one property of T must have a CustomAttribute of Type ColumnAttribute that .IsPrimaryKey == true and .DbType includes NOT NULL
  • Throws a NotSupportedException if the object supplied does not have a Primary Key
Usage:
//gets the IPrimaryKey of the Primary Key Column of the Products table
IPrimaryKey info = Naspinski.Utilities.LinqToSql.GetPrimaryKey<Product>(); 

//get the Type of the Primary Key of table Cars
TableKey carPrimaryKeyType = ((TableKey)Naspinski.Utilities.LinqToSql.GetPrimaryKey<Car>()).PropertyInfo.PropertyType;

//get the Primary Keys of a multi-keyed table
IEnumerable<TableKey> keys = ((TableMulitpleKeys)Naspinski.Utilities.LinqToSql.GetPrimaryKey<SomeObject>()).Keys;

GetCompositeKey()

TableCompositeKey GetCompositeKey<T>()
Details:
  • Gets the TableCompositeKey(s) of the supplied object T
  • T : class, INotifyPropertyChanged
  • At least two properties of T must have a CustomAttribute of Type ColumnAttribute that .IsPrimaryKey == true and .DbType includes NOT NULL
  • Throws a InvalidOperationException if the object supplied does not have multiple Primary Key
Usage:
//gets the primary keys for Widget
TableMultipleKey info = Naspinski.Utilities.LinqToSql.GetCompositeKey<Widget>();

GetSingleKey()

TableKey GetSingleKey<T>()
Details:
  • Gets the TableKey of the supplied object T
  • T : class, INotifyPropertyChanged
  • Only one property of T must have a CustomAttribute of Type ColumnAttribute that .IsPrimaryKey == true and .DbType includes NOT NULL
  • Throws a InvalidOperationException if the object supplied does not have only a single Primary Key
Usage:
//gets the primary keys for DooDad
TableKey info = Naspinski.Utilities.LinqToSql.GetSingleKey<DooDad>();

IsCompositeKey()

bool IsCompositeKey() [IPrimaryKey extension]
Details:
  • Returns true if the IPrimary key is a composite, false if not
  • IPrimaryKey
Usage:
bool keyIsComposite = Naspinski.Utilities.LinqToSql.GetPrimaryKey<Product>().IsCompositeKey();

Last edited Jan 7, 2015 at 6:46 AM by naspinski, version 8

Comments

No comments yet.